Android 在广播接收机内通知
在我的应用程序中,执行对远程数据库的调用,因此定期调用读取json响应,如果是特定值,则应发送通知。它使他的工作正常,但会打开发送通知的活动。我想知道的是,在内部控制中心,应用程序会发出通知,只有当用户点击它时,才会打开带有完整消息的活动。这可能吗? 这是我的密码Android 在广播接收机内通知,android,Android,在我的应用程序中,执行对远程数据库的调用,因此定期调用读取json响应,如果是特定值,则应发送通知。它使他的工作正常,但会打开发送通知的活动。我想知道的是,在内部控制中心,应用程序会发出通知,只有当用户点击它时,才会打开带有完整消息的活动。这可能吗? 这是我的密码 public class AlarmReceiver extends BroadcastReceiver { SharedPreferences sharedPreferences; String userid, datareq;
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();
}
}
}
这是使用帮助编辑的类
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
活动的其他部分
仅当用户单击通知时才打开此活动,请创建如下待定意图:
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);
Notification notification = new NotificationCompat.Builder(ctx).setContentIntent(pendingNotificationIntent).build();
然后使用NotificationCompat.Builder
设置此意图,如下所示:
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);
Notification notification = new NotificationCompat.Builder(ctx).setContentIntent(pendingNotificationIntent).build();
谢谢你的建议。不幸的是,我还不清楚。如果我将代码从NotificationView.Notify inside AlarmReceive.clas中移动,我将获得许多错误。那么,我必须在哪里使用通知=…等等?你能说清楚点吗?谢谢确保您正在导入所需的软件包。也可以使用
context
代替this
当然,我刚刚按照以下步骤测试了您的代码,但是如果我将代码private void Notify(String notificationTitle,String notificationMessage){NotificationManager NotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
内部AlarmReceiver getSistemservice是redflaggedUse,context.getSystemService()
完美。现在没有红旗错误。我已将该代码块移到AlarmReceiver内部,但当我使用notify(“msg”,“title”)时,它停止了。我认为它需要notificationcompat.builder,但我不知道如何使用它