问题描述
我想,即使在应用程序关闭该服务运行(kiiled),或者即使用户不启动应用程序。
我想服务启动安装应用程序后从这个角度,服务应始终运行。
I want that this service run even if the application is closed (kiiled) or even if the user dont start the app.i want the service start after the application is installed and from this point, the service should run always.
public class notifications extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
}
@Override
public void onStart(Intent intent, int startId) {
final Handler handler = new Handler();
final Runnable runb = new Runnable()
{
public void run()
{
Toast.makeText(getApplicationContext(), " Service Started", Toast.LENGTH_LONG).show();
handler.postDelayed(this, 10000);
}
};
handler.postDelayed(runb, 0);
}
@Override
public void onDestroy() {
}
}*/
public class notifications extends IntentService
{
private Timer mBackGroundTimer;
public notifications()
{
super("myservice");
this.mBackGroundTimer=new Timer();
}
@Override
protected void onHandleIntent(Intent intent)
{
// TODO Auto-generated method stub
mBackGroundTimer.schedule(new TimerTask()
{
public void run()
{
try
{
Notification("This is message from Dipak Keshariya (Android Application Developer)", "This is Android Notification Message");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},1000, 2000);
} // END onHandleIntent()
private void mStopTimer()
{
//Call this whenever you need to stop the service
mBackGroundTimer.cancel();
}
private void Notification(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message from Dipak Keshariya (Android Developer)!",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
}
我怎么能做到这一点?
how i can do that?
推荐答案
看你的code,看来你希望你的服务,定期给予通知。
Looking at your code, it appears you want your service to periodically give notifications.
至于有它连续运行得好,请记住,在设计上,Android系统可以在任何时候终止您的服务进程。你可以影响这个有点,但你不能杀死你的服务prevent系统。
As far as having it run continuously goes, keep in mind that by design, the Android system may terminate your service process at any time. You can influence this a bit, but you cannot prevent the system from killing your service.
因此,对于你的行为周期性,这将是最好使用带有AlarmManager一个反复出现的警报。然后,您的服务将主要是一次性的,即一次,然后执行的操作退出。
So for your periodical actions, it would be best to use AlarmManager with a recurring alarm. Your service would then basically be one-shot, i.e. perform the action once and then exit.
对于一些code,看看这里例如:
For some code, look here for example:
这篇关于运行服务即使在应用程序关闭(终止)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!