如何在 android 关闭应用程序的情况下获取通知
How to get notifications even app was closed in android
我正在开发一个每五秒触发一次通知的应用程序,但即使在应用程序关闭或终止时我也需要这样做,就像 whats 应用程序和 gmail 通知一样...运行 我的通知正在使用该服务,但是当我关闭或终止应用程序通知时,没有人能告诉我该怎么做
这就是我所做的
这是我的 activity:
package com.example.servicesandroid;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Timer timer;
Handler handler;
TimerTask timer_task;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
setContentView(R.layout.activity_main);
Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);
timer = new Timer();
timer_task = new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Androidservice.class);
MainActivity.this.startService(intent);
}
};
timer.schedule(timer_task, 5,5000);
这是我的服务:
package com.example.servicesandroid;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;
public class Androidservice extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
NotifyServiceReceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://sample.com/";
@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
// Send Notification
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Notification!";
String notificationText = "http://sample.com/";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return Service.START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class NotifyServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE) {
stopSelf();
}
}
}
}
您在 Main Activity 中使用计时器来启动通知。当您关闭您的应用程序时,计时器会停止,您的通知也会停止。在服务本身内部转移计时器的工作。这将帮助您在不依赖 Activity.
的情况下启动通知
您正在从 activity 右侧触发计时器任务。当应用程序关闭时 activity 被销毁,您的 task.Try 更改服务内的触发机制也会被销毁。
您似乎在每次启动计时器时都启动了服务。相反,将计时器逻辑转移到服务并仅启动服务一次。如果您需要更多说明,请询问。
我正在开发一个每五秒触发一次通知的应用程序,但即使在应用程序关闭或终止时我也需要这样做,就像 whats 应用程序和 gmail 通知一样...运行 我的通知正在使用该服务,但是当我关闭或终止应用程序通知时,没有人能告诉我该怎么做 这就是我所做的 这是我的 activity:
package com.example.servicesandroid;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Timer timer;
Handler handler;
TimerTask timer_task;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
setContentView(R.layout.activity_main);
Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);
timer = new Timer();
timer_task = new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Androidservice.class);
MainActivity.this.startService(intent);
}
};
timer.schedule(timer_task, 5,5000);
这是我的服务:
package com.example.servicesandroid;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.IBinder;
public class Androidservice extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
NotifyServiceReceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://sample.com/";
@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
// Send Notification
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,
"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Notification!";
String notificationText = "http://sample.com/";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return Service.START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class NotifyServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE) {
stopSelf();
}
}
}
}
您在 Main Activity 中使用计时器来启动通知。当您关闭您的应用程序时,计时器会停止,您的通知也会停止。在服务本身内部转移计时器的工作。这将帮助您在不依赖 Activity.
的情况下启动通知您正在从 activity 右侧触发计时器任务。当应用程序关闭时 activity 被销毁,您的 task.Try 更改服务内的触发机制也会被销毁。
您似乎在每次启动计时器时都启动了服务。相反,将计时器逻辑转移到服务并仅启动服务一次。如果您需要更多说明,请询问。