我的应用程序,执行对远程数据库的调用,因此定期调用以读取json响应,如果它是特定值,则应发送通知。它使他的工作正常,但打开发送通知的活动。我想要实现的是内部控制cicle,应用程序提供通知,只有当用户点击它时,打开活动并提供完整的消息。这可能吗? 这是我的代码
public class AlarmReceiver extends BroadcastReceiver {
SharedPreferences sharedPreferences;
String userid, datareq;
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences("My_App",
Context.MODE_PRIVATE);
String userid = prefs.getString("userid", "");
String datareq = prefs.getString("datareq", "");
String[] params = new String[2];
String[] result = new String[2];
params[0] = userid;
params[1] = datareq;
try {
result = new TaskControl(context.getApplicationContext()).execute(params).get();
if (result[1].equals("-1")) {
//failed
} else {
// Here i call NotificationView.class, but i should want send
// directly the notify
Intent msg = new Intent(context, NotificationView.class);
msg.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(msg);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是使用您的帮助的EDITED课程
public class AlarmReceiver extends BroadcastReceiver {
SharedPreferences sharedPreferences;
String userid, datareq;
private Context context;
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences("Beauty_App",
Context.MODE_PRIVATE);
String userid = prefs.getString("userid", "");
String datareq = prefs.getString("datareq", "");
String[] params = new String[2];
String[] result = new String[2];
params[0] = userid;
params[1] = datareq;
try {
result = new TaskControl(context.getApplicationContext()).execute(params).get();
if (result[1].equals("-1")) {
//failed
Toast.makeText(context, "failed", Toast.LENGTH_LONG).show();
} else {
// if i use notify, app stops
Notify ("Msg","Title");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.scanner,"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(context,NotificationView.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notification.setLatestEventInfo(context, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(9999, notification);
}
}
答案 0 :(得分:1)
将代码从NotificationView.Notify()
方法移至您开始NotificationView
活动的其他部分。
并且仅在用户点击通知创建待处理意图时才打开此活动,如下所示:
Intent intent = new Intent(this, NotificationView.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
然后使用NotificationCompat.Builder
设置此意图:
Notification notification = new NotificationCompat.Builder(ctx).setContentIntent(pendingNotificationIntent).build();