public class SimpleService extends Service {
private NotificationManager mNM;
private int NOTIFICATION = 0;
public class LocalBinder extends Binder {
SimpleService getService() {
return SimpleService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(NOTIFICATION);
// Tell the user we stopped.
Toast.makeText(this,"Service is destroy", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart(Intent intent, int startId) {
super.onCreate();
Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"task perform in service",300).show();
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting. We put an icon in the status bar.
showNotification();
return START_STICKY;
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
/**
* Show a notification while this service is running.
*/
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = getText(R.string.local_service_started);
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, text,
System.currentTimeMillis());
Intent intent = new Intent(this,NotificationRecieverActivity.class);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, "latest information",
text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
如果我點擊通知,然後該通知是明確的,但我不會顯示此通知消息到另一個活動即NotificationRecieverActivity.class但這不顯示任何內容。請幫幫我。
您應該閱讀本手冊,它並不總是像提供的簡單答案一樣線性(例如,您可能想要提供返回導航以維護應用程序體驗) http://developer.android.com /guide/topics/ui/notifiers/notifications.html – Philio