修改
您可以在评论中找到这个问题的答案!
发表
在我的应用程序中,我有计时器提醒用户他应该做的一些任务。时间到了,会发送通知。当用户点击此通知时,我希望将应用放在前台,如果不是这样的话。我坚持认为
'app'因为我发现很多帖子显示如何将Activity
放在前台,这不是这里的情况。
以下是当我想发送通知并将我的应用程序带到前台时调用的Broadcastreceiver的代码:
public class TimerBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int ID = 333;
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.clock_alarm2);
builder.setContentTitle("Time is up");
builder.setContentText("SLIMS");
builder.setOnlyAlertOnce(false);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
builder.setVibrate(new long[] { 0, 200, 3000 });
ActivityManager activityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(100);
for (int i = 0; i < taskInfo.size(); i++) {
String componentName = taskInfo.get(i).topActivity.getClassName();
if (componentName.contains("com.genohm.android")) {
Intent intentActivity = new Intent();
intentActivity.setComponent(taskInfo.get(i).topActivity);
intentActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(contentIntent);
}
}
final Notification notification = builder.build();
notification.flags |= Notification.FLAG_INSISTENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager mNM = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNM.notify(ID, notification);
}
}
此代码完美无缺。问题是,我正在使用方法getRunningTasks(),谷歌强烈建议强烈劝阻。
此类用途不受支持,将来可能会中断。
但实际上这是我到目前为止找到的唯一方式。 有谁知道这种做法是否存在某些解决方法?