在android手机重新启动我的服务

编程入门 行业动态 更新时间:2024-10-28 19:33:06
本文介绍了在android手机重新启动我的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在做应用程序,它会持续跟踪用户的位置,到目前为止,我已经能够使其上的位置变化纵成功接收,但如果他重新启动手机不是我不能够重新开始我的服务,而无需用户打开应用程序。

I am making app which tracks user location continuously, so far i have been able to make a successful receiving of its ordinate on location change, but if he is restarting the phone than i am not able to start my service without user again opening the app.

  • 的Manifest.xml

  • Manifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/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

  • 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

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

  • 我要去的地方错了任何帮助将是pciated在手机上重新启动,我得到demoapp已经停止我试图把吐司上reciever但面包还没有到来,在present高度AP $ P $

    where i am going wrong any help would be highly appreciated on restart of a phone i get demoapp has stopped i tried putting toast on reciever but that toast also are not coming at present

    推荐答案

    请确保u必须在AndroidManifest文件的权限:

    Make Sure u have the permission in AndroidManifest File :

    <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手机重新启动我的服务

    本文发布于:2023-11-27 02:45:43,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1636241.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重新启动   手机   android

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!