Android 即使应用程序已关闭(终止),也要运行服务

Android 即使应用程序已关闭(终止),也要运行服务,android,service,background,Android,Service,Background,我希望即使应用程序已关闭(kiiled)或用户未启动应用程序,此服务也能运行。 我希望服务在安装应用程序后启动,从这一点上说,服务应该始终运行 public class notifications extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub

我希望即使应用程序已关闭(kiiled)或用户未启动应用程序,此服务也能运行。 我希望服务在安装应用程序后启动,从这一点上说,服务应该始终运行

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

我如何才能做到这一点?

因为您的要求是在后台运行服务。您使用该服务是正确的,因为它仅用于后台运行

从活动中,您可以通过以下方式启动服务:

startService(new Intent(activityName.this, serviceName.class));
或者,如果您的应用程序没有任何活动,那么您可以通过将

<service android:name="name of the service" >
 <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </service>

查看您的代码,您似乎希望您的服务定期发出通知

至于让它持续运行,请记住,根据设计,Android系统可能随时终止您的服务进程。您可以稍微影响一下,但无法阻止系统终止您的服务

因此,对于您的定期操作,最好将AlarmManager与定期报警一起使用。您的服务基本上是一次性的,即执行一次操作,然后退出

有关某些代码,请参见以下示例:


我想这个问题的答案是:


Google I/O 2013中引入的同步适配器

您需要实现服务类的OnStartCommand方法,并在其中返回Service.START\u。那就行了。如果您终止应用程序,服务将继续在后台运行。但是,如果您重新启动手机,我认为您需要在应用程序中实现其他功能,以及启动服务或类似功能。

阅读这一项和这一项是的,但如果我关闭应用程序或重新启动智能手机,服务将停止工作,但我希望他总是运行..关闭应用程序和重启设备是两码事。当手机再次启动时,您可以放置一个广播接收器,即可启动服务。并使您的服务具有粘性,这样系统就不会终止它。因此,我将它放在我的mainactivity上,它将启动服务,即使我终止应用程序,服务也不会停止运行?它将注册一个警报,该警报将定期启动您的服务。您的服务不需要一直运行(事实上,您的服务应该在您执行了一次所需的操作之后才退出),因为您可以设置警报以在需要时触发单个服务运行。您的应用程序也不需要运行。我希望在X追加时发送通知,但X位于网站(json)上。因此,我需要始终运行该服务,然后如果X追加,我希望发送通知。它需要一直运行。即使我没有启动应用程序。好吧,使用alarm manager方法,您的服务也不需要一直运行。执行服务时需要做的唯一一件事是检查您的条件X一次,如果为true,则发送通知。alarm manager将负责每x秒调用一次您的服务,这与让您的服务始终运行基本相同。好处是,您不必担心android系统会扼杀您的服务进程,因为每次需要完成任务时,它都会重新启动。即使你的应用程序没有运行,因为alarm manager不依赖你的应用程序。我认为你不能在不先启动应用程序的情况下启动服务。但使用START_STICKY,在您启动服务后,它不会停止。但我的要求是,我必须在应用程序START.ex登录后启动服务。