0

バックグラウンド サービスを使用するアプリケーションがあります。モバイルの電源がオフになっている場合、サービスはオフになっています。電話の起動時にサービスを再起動しようとしていますが、開始されません。誰でも私を助けることができますか?私はこのコードを使用しています:

  <receiver android:name=".ConnectionReceiver"
        android:enabled="true" android:exported="false" >
          <intent-filter>        
          <action android:name="android.intent.action.BOOT_COMPLETED" /> 
             </intent-filter>
             </receiver> 
  </application>
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

onReceive() 関数を更新する

    public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Intent i = new Intent();
                i.setAction("com.android.ConnectionReceiver");
                context.startService(i);
            }
                NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                if(null != info)
                {
                        String state = getNetworkStateString(info.getState());
                        if(state.equals("Connected")){
                            mTimer = new Timer();
                            mTimerTask = new TimerTask() {
                                @Override
                                public void run() {
                                    loginForm.this.runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {


                                                    insertAllGps();



                                    });

                               }
                            };
                            mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
4

1 に答える 1

0

// equals use equalsIgnoreCase の代わりにこの行を使用

if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction()))
于 2012-01-14T10:22:21.640 に答える