Android 通过通知将变量传递给活动
以下是我的代码: gcminentservice:Android 通过通知将变量传递给活动,android,notifications,Android,Notifications,以下是我的代码: gcminentservice: Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra("screen", activityToSh
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", activityToShow);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(smallIconToDisplayInGCM)
.setLargeIcon(largeIconToDisplayInGCM)
.setContentTitle(title)
.setContentText(text)
.setSound(sound)
.setTicker(ticker)
.setContentIntent(intent2)
.setAutoCancel(true);
主要活动:
onCreate()
onCreate()之外的新方法
当我点击推送通知时,它甚至没有点击Log.d()
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", "debugScreen");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationManager nMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Some Title")
.setContentText("Some text")
.setContentIntent(intent2)
.setAutoCancel(true);
nMgr.notify(0, nBuilder.build());
以及onNewIntent方法:
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String tabNumber;
if(extras != null) {
tabNumber = extras.getString("screen");
Log.d("TEMP", "Tab Number: " + tabNumber);
} else {
Log.d("TEMP", "Extras are NULL");
}
}
我可以检查它是否正常工作,变量是否传递给intent中的活动
我认为您可能做错的是使用变量activityToShow设置意图数据。考虑到Intent.putExtra(…)是一个重载方法,如果您传递一个整数,您将无法获取字符串
我建议您监控活动toshow变量的状态,并向接收器添加一些日志记录,以检查您得到了什么(包括变量类型)以及您在活动中收到了什么
希望有帮助。我在活动中调用了onNewIntent()方法-
MainActivity.java
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.putExtra("screen", "debugScreen");
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationManager nMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent2 = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Some Title")
.setContentText("Some text")
.setContentIntent(intent2)
.setAutoCancel(true);
nMgr.notify(0, nBuilder.build());
@Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String tabNumber;
if(extras != null) {
tabNumber = extras.getString("screen");
Log.d("TEMP", "Tab Number: " + tabNumber);
} else {
Log.d("TEMP", "Extras are NULL");
}
}