Android 当应用程序处于后台时发送通知
当我的应用程序处于后台时,我正在发送通知。当应用程序处于后台时,通知正在发送,但当我单击通知并打开应用程序时,应用程序正在崩溃。我需要发送通知时,应用程序只是在后台,我一直这样做的iBeacon开发Android 当应用程序处于后台时发送通知,android,Android,当我的应用程序处于后台时,我正在发送通知。当应用程序处于后台时,通知正在发送,但当我单击通知并打开应用程序时,应用程序正在崩溃。我需要发送通知时,应用程序只是在后台,我一直这样做的iBeacon开发 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
;
new ForegroundCheckTask().execute();
}
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
@Override
protected Boolean doInBackground(Context... params) {
postNotificationUBCity("Entered region");
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
为了这个目的,我不会选择AsyncTask 如果要在应用程序处于后台模式时发送通知,请尝试在onPause方法中使用以下代码: 添加适当的通知:
private void addNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
不要忘记在您的AndroidManifest中包含权限:
<uses-permission android:name="android.permission.GET_TASKS" />
这应该行。应用程序正在崩溃。那么Logcat在哪里?请共享你的应用程序崩溃日志。请检查Logcat..AddPostNotification的代码,我不明白你在onCreate中调用这个asyncTask,那么它在“后台”中是如何调用的?@Darpan:我应该在哪里调用asyncTask?
private void addNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
String appname = context.getResources().getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, myactivity.class), 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(appname).setWhen(0)
.setAutoCancel(true).setContentTitle(appname)
.setContentText(message).build();
notificationManager.notify(0 , notification);
}
<uses-permission android:name="android.permission.GET_TASKS" />