Вопрос

I have an application using a background service. If my mobile is switched off, my service is off. I am trying to restart the service when the phone boots, but it's not starting. Can anybody help me? I am using this code:

  <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" />

update onReceive() function

    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);
Это было полезно?

Решение

//use this line instead of equals use equalsIgnoreCase

if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction()))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top