Android:通知导航

时间:2015-04-20 14:45:58

标签: android

我正在设计Android游戏。当用户在某个时间不是我的游戏玩家时,游戏将生成通知。

问题:  当用户自动点击通知时,必须触发游戏。你能帮我解决一下这个问题吗?

public class AndroidLauncher extends AndroidApplication implements ActionResolver {

AlarmManager am;

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    initialize(new MainGame(this), config);
}

@Override
public void showOrLoadInterstital() {
    // TODO Auto-generated method stub

}

@Override
public void sendToGoogleAnalytics(String arg) {
    // TODO Auto-generated method stub

}

@Override
public void setNotification(String msg) {
    // TODO Auto-generated method stub 
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      setOneTimeAlarm(msg);
}

public void setOneTimeAlarm(String msg) {
  Intent intent = new Intent(this, TimeAlarm.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);
  am.set(AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis() + (1 * 1000), pendingIntent);  
} 

}

TimeAlarm

public class TimeAlarm extends BroadcastReceiver {

 NotificationManager nm;

 @Override
 public void onReceive(Context context, Intent intent) {

  int currentapiVersion = android.os.Build.VERSION.SDK_INT;
  nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  

  PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);      

     String heading = "Content Heading";
     String message = "Message";

     String tickerHeading="Ticker Heading";

     if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
         Notification n; 
         n = new Notification(R.drawable.notification_icon_72_72, " API < 11 Msg", 0);
         n.setLatestEventInfo(context, heading , message, contentIntent);  
         n.flags = Notification.FLAG_AUTO_CANCEL;
         nm.notify(0, n);
     }else{
          Resources res = context.getResources();
          NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
          builder.setContentIntent(contentIntent)
          .setSmallIcon(R.drawable.notification_icon_72_72)
          .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon_114_114))
          .setTicker(tickerHeading)
          .setWhen(System.currentTimeMillis())
          .setAutoCancel(true)
          .setContentTitle( heading)
          .setContentText(message);
          Notification n = builder.build();   
          nm.notify(1, n); 
     }  

 }
}

enter image description here

1 个答案:

答案 0 :(得分:2)

假设您已经有一个地方,在一段时间后调用代码,这应该是其中的内容:

NotificationManager mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent i = new Intent(context, ActivityToLaunch.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.my_icon)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.my_large_icon))
                        .setAutoCancel(true)
                        .setContentTitle("notification title")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("message of notification"))
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setTicker("message of notificaiton in status bar")
                        .setContentText("message of notificaiton");

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());