gpt4 book ai didi

android - 在 android 手机重启时启动我的服务

转载 作者:行者123 更新时间:2023-11-29 17:57:32 25 4
gpt4 key购买 nike

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

  • list .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);
    }
    }
    }
  • 后台服务.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 已停止,我尝试将 toast 放在接收器上,但该 toast 目前也没有出现

最佳答案

确保你在 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);
}

关于android - 在 android 手机重启时启动我的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com