简体   繁体   English

引导设备后服务未启动

[英]Service not starting after boot device

I have a problem with running the service. 我在运行服务时遇到问题。 In the logs there is nothing to see that the service is running. 在日志中,看不到该服务正在运行。 So I don't know that she works. 所以我不知道她在工作。 Toast also does not show in MainActivity. Toast也不会显示在MainActivity中。 I read a lot of posts and none work. 我读了很多文章,却没有任何工作。 How to fix it? 如何解决?

Service 服务

import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;

public class AutoStartUp extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        // do something when the service is created
    }

}

BroadcastReceiver 广播接收器

import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;

public class BootComplete extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, AutoStartUp.class);
            context.startService(serviceIntent);
        }

    }
}

Manifest.xml Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kamiszczu.ovh.servicetest3">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
    </uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name="kamiszczu.ovh.servicetest3.BootComplete"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service
            android:name="kamiszczu.ovh.servicetest3.AutoStartUp"
            android:enabled="true">
        </service>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

You don't have to verify the intent type since the Receiver is registered with only one intent type(Intent.ACTION_BOOT_COMPLETED). 您不必验证意图类型,因为Receiver仅注册了一种意图类型(Intent.ACTION_BOOT_COMPLETED)。 So there is no need to check in the receiver if the intent action is Intent.ACTION_BOOT_COMPLETED. 因此,无需检查接收方的意图动作是否为Intent.ACTION_BOOT_COMPLETED。 I think the condition in the receiver is not true and because of that the code that starts your service is not executed. 我认为接收器中的条件不正确,因此启动您的服务的代码未执行。

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

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