如何处理 android 通知警报中的多个意图操作
How to handle multiple intent actions in android notification alarm
我的查询是我正在尝试根据不同的条件重定向通知意图,我正在处理警报系统,我需要将通知意图重定向到某个 activity 说 A。如果用户在闹钟播放期间点击它,完成后,通知应该重定向到其他 activity 如果那里仍然可用。这是我的代码。任何帮助将不胜感激!
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());
}
您必须从通知中触发广播接收器,并且在 BroadcastReceiver
的 onReceive()
方法中您必须决定从哪个 activity 开始....
更新
像这样写一个 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);
}
并像这样创建 Pending 意图
Intent myIntent=new Intent("some_action")
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我的查询是我正在尝试根据不同的条件重定向通知意图,我正在处理警报系统,我需要将通知意图重定向到某个 activity 说 A。如果用户在闹钟播放期间点击它,完成后,通知应该重定向到其他 activity 如果那里仍然可用。这是我的代码。任何帮助将不胜感激!
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());
}
您必须从通知中触发广播接收器,并且在 BroadcastReceiver
的 onReceive()
方法中您必须决定从哪个 activity 开始....
更新
像这样写一个 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);
}
并像这样创建 Pending 意图
Intent myIntent=new Intent("some_action")
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);