移动设备开机后,在Android中启动BroadcastReceiver?

时间:2012-01-14 09:46:39

标签: android restart background-service

我有一个使用后台服务的应用程序。如果我的手机已关闭,我的服务已关闭。我试图在手机启动时重新启动服务,但它没有启动。有谁能够帮我?我正在使用此代码:

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

1 个答案:

答案 0 :(得分:0)

//使用此行而不是equals使用equalsIgnoreCase

if (Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction()))