简体   繁体   English

为什么广播接收器不适用于服务应用程序android?

[英]why Broadcast Receiver Don't work for service application android?

I have a project which is only a service and it has no activity and user interface. 我有一个仅是服务的项目,没有任何活动和用户界面。 I want to start my application background service when phone boot completely. 我想在电话完全启动后启动我的应用程序后台服务。 but I never receive the "BOOT_COMPLETED" Message from OS. 但我从未收到来自操作系统的“ BOOT_COMPLETED”消息。 these are my code: 这些是我的代码:

Manifest: 表现:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name=".BootBroadcastReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
    </intent-filter>

    </receiver>
</application>

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

Broadcast Receiver: 广播接收器:

public class BootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("boot Received", intent.getAction());
        Intent serviceLuncher = new Intent(context, myService.class);
        context.startService(serviceLuncher);
    }
}

myService: myService:

public class LocationNotifierService extends Service {
   Timer timer ;
   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   @Override
   public void onCreate(){
       timer = new Timer();
       timer.schedule(new TimerTask() {
       @Override
       public void run() {

 Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
          }
          },3000);
      }

    @Override
    public void onDestroy(){

    }
    @Override
    public int onStartCommand(Intent intent, int flagId, int startId){
        return START_STICKY;
    }
}

but I never get "boot Received" log. 但是我从来没有收到“ boot Received”日志。 is there any mistake and is there any way to debug my program? 有什么错误,有什么方法可以调试我的程序吗?

I Recommend that my project must have only this Service and it cannot have any UI. 我建议我的项目必须仅具有此服务,并且不能具有任何UI。

I never receive the "BOOT_COMPLETED" Message from OS 我从未收到来自操作系统的“ BOOT_COMPLETED”消息

Partly, that is because you do not have a <receiver> set up to receive android.intent.action.BOOT_COMPLETED broadcasts. 部分原因是,您没有设置<receiver>来接收android.intent.action.BOOT_COMPLETED广播。

Partly, that is because your app will not receive broadcasts until something on the device uses an explicit Intent to start one of your components. 部分原因是,直到设备上的某些内容使用显式的Intent启动您的组件之一,您的应用程序才会接收广播。 The way your app is set up — without an activity that the user can run — it is unlikely that any app will do this, and so your code will never run. 应用程序的设置方式-没有用户可以运行的活动-几乎没有任何应用程序可以这样做,因此您的代码将永远无法运行。

Also, please bear in mind that Android O has changes designed specifically to prevent background services from running for very long and to limit your ability to get background location updates (which your location_tracker name suggests that you want to add in the future). 另外,请记住,Android O进行了专门设计的更改,以防止后台服务长时间运行,并限制您获取后台位置更新的能力(您的location_tracker名称建议您将来添加)。 You may wish to reconsider whether writing this app the way that you are is a wise course. 您可能希望重新考虑是否以自己的方式编写此应用是明智的选择。

try this in your manifest 试试你的清单

 <receiver android:name=".BootBroadcastReceiver"
    android:enabled="true"
    android:exported="false"
    android:label="StartServiceAtBootReceiver">
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
       </intent-filter>
    </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM