在Android手机重启时启动我的服务

时间:2013-08-18 13:41:08

标签: android boot restart

我正在制作连续跟踪用户位置的应用程序,到目前为止,我已经能够成功接收其位置更改的纵坐标,但是如果他重新启动手机而不是我无法再次启动我的服务打开应用程序。

  • 的Manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aa.gpsdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
    
    <uses-permission android:name="android.permission.INTERNET"/>
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>   
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    
    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.aa.gpsdemo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyService" android:label="My Service">
        <intent-filter>
        <action android:name="com.aa.gpsdemo.StartMyServiceAtBootReciever" />
        </intent-filter>
    </service>
    <receiver
        android:name=".receiver.StartMyServiceAtBootReceiver"
        android:enabled="true"
        android:exported="true"
        android:label="StartMyServiceAtBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver> 
        </application>
    
        </manifest>
    
  • StartMyServiceAtBootReciever.java

    package com.aa.gpsdemo;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class StartMyServiceAtBootReciever extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent serviceIntent = new Intent("com.aa.gpsdemo.BackgroundService");
        context.startService(serviceIntent);
          }
        }
     } 
    
  • BackgroundService.java

    package com.aa.gpsdemo;
    
    import android.app.Service;
    import android.content.Intent;
    import android.content.Context;
    import android.os.IBinder;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.util.Log;
    import android.widget.Toast;
    import android.location.Criteria;
    
    public class BackgroundService extends Service {
    
    private static NewLocationListener mylistner=null;
    private final static String TAG = "MainActivity";
    private Context context = null;
    
    @Override
    public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    
    mylistner = new NewLocationListener(context);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
    criteria.setCostAllowed(true);
    LocationManager locManager = (LocationManager)     context.getSystemService(Context.LOCATION_SERVICE);
    String bestProvider=locManager.getBestProvider(criteria, true);
    locManager.requestLocationUpdates(bestProvider,1000, 5, mylistner);
    
    
    
    }
    
        }
    

我出错了任何帮助都会在重新启动手机时受到高度赞赏我得到了demoapp已经停止我尝试在接收器上烤面包但是烤面包也不会出现在目前

2 个答案:

答案 0 :(得分:3)

确保您拥有AndroidManifest文件中的权限:

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

将其更改为:

@Override
public void onReceive(Context context, Intent intent) {
    Intent serviceIntent = new Intent(context,BackgroundService.class);
    startService(serviceIntent);
    }

答案 1 :(得分:0)

RECEIVE_BOOT_COMPLETED :广播操作:系统完成引导后,播放一次。它可用于执行特定于应用程序的初始化,例如安装警报。您必须持有RECEIVE_BOOT_COMPLETED权限才能接收此广播。

Refrence

Stackoverflow Question