如何在活动开始时运行通知以及如何在活动恢复时阻止它再次启动

时间:2014-06-13 03:00:57

标签: android notifications

我有一个简单的活动,可以调用通知但我回到活动时会遇到问题再次运行通知如何阻止它再次运行我在下面有这个代码

主要活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
notification();

    }
public void notification(){
        // Invoking the default notification service
          NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);  

          mBuilder.setContentTitle("New Message with explicit intent");
          mBuilder.setContentText("New message from javacodegeeks received");
          mBuilder.setTicker("Explicit: New Message Received!");
          mBuilder.setSmallIcon(R.drawable.banner);

          // Increase notification number every time a new notification arrives 
          mBuilder.setNumber(++numMessagesOne);

          // Creates an explicit intent for an Activity in your app 
          Intent resultIntent = new Intent(this, NotesActivity.class);
          resultIntent.putExtra("notificationId", notificationIdOne);

          //This ensures that navigating backward from the Activity leads out of the app to Home page
          TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

          // Adds the back stack for the Intent
          stackBuilder.addParentStack(NotesActivity.class);

          // Adds the Intent that starts the Activity to the top of the stack
          stackBuilder.addNextIntent(resultIntent);
          PendingIntent resultPendingIntent =
             stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_ONE_SHOT //can only be used once
             );
          // start the activity when the user clicks the notification text
          mBuilder.setContentIntent(resultPendingIntent);

          myNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

          // pass the Notification object to the system 
          myNotificationManager.notify(notificationIdOne, mBuilder.build());  

       }
}

此外,我想知道如何对同一种事件和不同的事件进行多次通知,例如我从javacodegeeks得到2条消息然后数字应该增加,如果我从不同的用户得到消息,那么我得到两个不同的通知两个不同的消息?这是我获得本教程的链接

http://examples.javacodegeeks.com/android/core/ui/notifications/android-notifications-example/

0 个答案:

没有答案