Android Notification通知的运用
package gongzibai.co.cc;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class EA2Activity extends Activity {
private NotificationManager mNotificationManager;
private Spinner mSpinner;
private ArrayAdapter<String> mAdapter;
private static final String[] status = {
"在线", "离开", "回家", };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mSpinner = (Spinner) findViewById(R.id.spinner1);
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, status);
mSpinner.setAdapter(mAdapter);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if (status[arg2].equals("在线")) {
setNotiType(R.drawable.ic_launcher, "在线了");
} else if (status[arg2].equals("离开")) {
setNotiType(R.drawable.ic_launcher, "离开了");
} else if (status[arg2].equals("回家")) {
setNotiType(R.drawable.ic_launcher, "回家了");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private void setNotiType(int iconID, String string) {
// TODO Auto-generated method stub
PendingIntent intent = PendingIntent.getActivity(EA2Activity.this, 0,
new Intent(EA2Activity.this, EA2Activity.class), 0);
Notification mNotification = new Notification();
mNotification.tickerText = string;
mNotification.icon = iconID;
mNotification.defaults = Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE;
mNotification.setLatestEventInfo(EA2Activity.this, "登录状态", string,
intent);
mNotificationManager.notify(3, mNotification);
}
}
--------------------------
通知的应用一定要设置一个图片,否则通知在状态栏只有提示声音,没有文字的出现..
震动的通知,在权限增加震动权限.