gpt4 book ai didi

android - 在 BroadcastReceiver 中通知

转载 作者:行者123 更新时间:2023-11-30 01:44:18 25 4
gpt4 key购买 nike

我的应用程序,执行对远程数据库的调用,因此定期调用以读取 json 响应,如果它是特定值,则应发送通知。它使他的工作正常,但打开发送通知的 Activity 。我应该意识到的是,在控制 cicle 内部,应用程序会发出通知,只有当用户点击它时,才会打开带有完整消息的 Activity 。这可能吗?这是我的代码

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();
}

}

}

这是使用您的帮助编辑的类(class)

   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);
}

最佳答案

将您的代码从 NotificationView.Notify() 方法移动到您开始 NotificationView Activity 的其他部分。

并且仅当用户点击通知时才打开此 Activity ,创建如下所示的未决 Intent :

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 设置此 Intent ,如下所示:

Notification notification = new NotificationCompat.Builder(ctx).setContentIntent(pendingNotificationIntent).build();

关于android - 在 BroadcastReceiver 中通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33957137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com