我的查询是我试图根据不同的条件重定向通知意图,我在警报系统上工作,我需要将通知意图重定向到某个活动,例如A。如果用户在警报播放期间或之后单击它,完成后,如果该通知仍然可用,则应将其重定向到其他活动。这是我的代码。任何帮助将不胜感激!
private void DisplayNotification(String AlarmName, Context context) {
// Context context = context.getApplicationContext();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent;
SharedPreferences alarmindicator = context.getSharedPreferences(
"notifyintent", context.MODE_PRIVATE);
int code;
SharedPreferences.Editor editor = alarmindicator.edit();
if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
mIntent = new Intent(context, AlarmAlertActivity.class);
Toast.makeText(context, "inalmact", 0).show();
code = 1;
}
else {
editor.putBoolean("notifyintentPlaying", true);
editor.commit();
mIntent = new Intent(context, MainActivity.class);
// AlarmAlertActivity.this.finish();
code = 2;
Toast.makeText(context, "in main act", 0).show();
}
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(context, code,
mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
builder.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.notifyiconlarg)
.setLargeIcon(
BitmapFactory.decodeResource(res,
R.drawable.notifyiconlarg))
// .setTicker(res.getString(R.string.notification_title))
.setTicker(AlarmName).setAutoCancel(true)
.setContentTitle(AlarmName)
.setContentText("Time to offer " + AlarmName + " Prayers");
notificationManager = (NotificationManager) context
.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(123, builder.build());
}
您必须从通知中解雇广播接收器,并且必须使用哪种onReceive()
方法BroadcastReceiver
来决定要开始哪个活动...。
更新
像这样写一个BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int code;
Intent mIntent;
SharedPreferences.Editor editor = alarmindicator.edit();
if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
mIntent = new Intent(context, AlarmAlertActivity.class);
Toast.makeText(context, "inalmact", 0).show();
code = 1;
} else {
editor.putBoolean("notifyintentPlaying", true);
editor.commit();
mIntent = new Intent(context, MainActivity.class);
// AlarmAlertActivity.this.finish();
code = 2;
Toast.makeText(context, "in main act", 0).show();
}
context.startActivity(intent);
}
并像这样创建待处理的意图
Intent myIntent=new Intent("some_action")
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句