72

サービスを作成しましたが、電話が再起動するか強制終了されるまで、常にこのサービスを実行したいと考えています。サービスはバックグラウンドで実行する必要があります。

作成されたサービスと開始サービスのサンプル コード:

サービスを開始します。

Intent service = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(service);

サービス:

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO do something useful
        HFLAG = true;
        //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO for communication return IBinder implementation
        return null;
    }
}

マニフェスト宣言:

<service
    android:name=".MyService"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
</service>

アプリケーションが一時停止したときなど、常にこのサービスを実行することは可能ですか? しばらくすると、アプリケーションが一時停止し、サービスも一時停止または停止します。では、このサービスをバックグラウンドで常に実行するにはどうすればよいでしょうか。

4

9 に答える 9

93

「アプリケーションが一時停止したときなどに、このサービスを常に実行することは可能ですか?」

はい。

  1. サービスの onStartCommand メソッドで START_STICKY を返します。

    public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
    }
    
  2. startService(MyService) を使用してバックグラウンドでサービスを開始し、バインドされたクライアントの数に関係なく常にアクティブな状態を維持します。

    Intent intent = new Intent(this, PowerMeterService.class);
    startService(intent);
    
  3. バインダーを作成します。

    public class MyBinder extends Binder {
            public MyService getService() {
                    return MyService.this;
            }
    }
    
  4. サービス接続を定義します。

    private ServiceConnection m_serviceConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName className, IBinder service) {
                    m_service = ((MyService.MyBinder)service).getService();
            }
    
            public void onServiceDisconnected(ComponentName className) {
                    m_service = null;
            }
    };
    
  5. bindService を使用してサービスにバインドします。

            Intent intent = new Intent(this, MyService.class);
            bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
    
  6. サービスが閉じられたら、適切なアクティビティを起動する通知が必要な場合があります。

    private void addNotification() {
            // create the notification
            Notification.Builder m_notificationBuilder = new Notification.Builder(this)
                    .setContentTitle(getText(R.string.service_name))
                    .setContentText(getResources().getText(R.string.service_status_monitor))
                    .setSmallIcon(R.drawable.notification_small_icon);
    
            // create the pending intent and add to the notification
            Intent intent = new Intent(this, MyService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
            m_notificationBuilder.setContentIntent(pendingIntent);
    
            // send the notification
            m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build());
    }
    
  7. アクティビティをシングル トップ モードで起動するには、マニフェストを変更する必要があります。

              android:launchMode="singleTop"
    
  8. システムがリソースを必要としていて、サービスがあまりアクティブでない場合、強制終了される可能性があることに注意してください。これが受け入れられない場合は、startForeground を使用してサービスをフォアグラウンドに移動します。

            startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
    
于 2013-04-02T23:00:54.373 に答える
8

独自のプロセスでサービスを開始するには、xml 宣言で次のように指定する必要があります。

<service
  android:name="WordService"
  android:process=":my_process" 
  android:icon="@drawable/icon"
  android:label="@string/service_name"
  >
</service> 

ここで、私にとって本当に役立つ優れたチュートリアルを見つけることができます

http://www.vogella.com/articles/AndroidServices/article.html

お役に立てれば

于 2013-04-02T08:22:22.343 に答える
3

簡単な解決策は、システムがサービスを停止したときにサービスを再起動することです。

このメソッドの非常に単純な実装を見つけました。

Androidサービスを止められないようにする方法

于 2016-07-15T16:05:28.287 に答える
3

すでにサービスがあり、それを常に機能させたい場合は、2 つのことを追加する必要があります。

  1. サービス自体で:

    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    
  2. マニフェストで:

    android:launchMode="singleTop"
    

サービスで必要でない限り、バインドを追加する必要はありません。

于 2016-11-20T04:45:07.700 に答える
2

サービスに実装できstartForeground、サービスが停止した場合でもSTART_STICKYonを使用して再起動できますstartCommand()。これが正しい実装かどうかはわかりません。

于 2013-04-02T08:25:01.500 に答える
0

ブロードキャスト レシーバーは必要ありません。Stephen Donecker による上記の例から api(serviceconnection) の 1 つをコピーして Google に貼り付けると、https: //www.concretepage.com/android/android-local-bound-service- example-with-binder-and-serviceconnection

于 2018-10-27T09:06:47.833 に答える