我的Android应用程序中有本地通知。它位于主要活动中。它使用字符串数组来填充上下文文本字段。我还使用一个名为notifCount的整数,每次通知执行时都会递增。当本地通知显示在顶部时,它会更改其上下文文本。我的问题是当我在onCreate中调用pendingIntent调用的活动中的notifCount时,似乎它显示了活动并且未加载活动上的onCreate。
这是我的主要活动的一部分,它启动了当地的通知;
public void showNotif(){
contentTxt = arrContentTxt[notifCount];
Log.d("Batelec", "main notifCount: "+notifCount);
Bundle b = new Bundle();
b.putInt("notifCountNum", notifCount);
Uri notifSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent i = new Intent(MainActivity.this, NotifReceiver.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0);
Notification notif = new Notification.Builder(this)
.setContentTitle("MyPower Reminder")
.setContentText(contentTxt)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.setSound(notifSound)
.addAction(0, "View Full Reminder", pi)
.build();
i.putExtras(b);
NotificationManager notifMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifMgr.notify(0, notif);
notifCount++;
if(notifCount == contentTxtLength){
notifCount = 0;
}
}
这是我当地的通知接收者;
package com.example.mypower_build101;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class NotifReceiver extends Activity {
TextView txtMsg, txtTitle;
Bundle b;
int notifCount;
String[] fullTxt, fullTitle;
String notifFullTxt, notifFullTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notif_receiver);
txtMsg = (TextView)findViewById(R.id.txtMsg);
txtTitle = (TextView)findViewById(R.id.txtTitle);
Resources res = getResources();
fullTitle = res.getStringArray(R.array.notifContentText);
//fullTxt = res.getStringArray(R.array.notifFullTxt);
b = getIntent().getExtras();
try{
notifCount = b.getInt("notifCountNum");
Log.d("Batelec", "receiver notifCount: "+notifCount);
}catch(Exception e){
Log.d("Batelec", "notifReceiver error: "+e);
}
if(b != null){
notifFullTitle = fullTitle[notifCount];
//notifFullTxt = fullTxt[notifCount];
Log.d("Batelec", "notifCount: "+notifCount);
txtTitle.setText(""+notifFullTitle);
//txtMsg.setText(""+notifFullTxt);
}
else{
Log.d("Batelec", "b null");
}
}
}
答案 0 :(得分:0)
Intent i = new Intent(MainActivity.this, NotifReceiver.class);
i.putExtras(b); // add here
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, (int)(Math.random*100), i, 0);
Notification notif = new Notification.Builder(this)
.setContentTitle("MyPower Reminder")
.setContentText(contentTxt)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.setSound(notifSound)
.addAction(0, "View Full Reminder", pi)
.build();
NotificationManager notifMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notifMgr.notify(notifCount, notif); //instead of zero, write notifCount