15

アプリケーションが閉じられている (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);
        }
}

どうすればそれができますか?

4

4 に答える 4

10

コードを見ると、サービスに定期的に通知を送信する必要があるようです。

継続的に実行する限り、Android システムは設計上、いつでもサービス プロセスを終了できることに注意してください。これに少し影響を与えることはできますが、システムがサービスを強制終了するのを防ぐことはできません。

そのため、定期的なアクションについては、AlarmManager と繰り返しアラームを使用するのが最適です。その場合、サービスは基本的にワンショットになります。つまり、アクションを 1 回実行してから終了します。

いくつかのコードについては、たとえばここを見てください:

Android: アラーム マネージャー

于 2013-09-16T11:55:59.257 に答える
1

この質問への答えはこれだと思います: https://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Google I/O 2013 で導入された同期アダプター。

于 2015-05-10T20:06:08.943 に答える