【问题标题】:Service does not start when the phone is started手机启动时服务不启动
【发布时间】:2019-05-16 15:29:51
【问题描述】:

我对 android 编程很陌生。我正在尝试在手机启动时启动服务,但它不起作用。我已经看到其他用户完成了其他问题,但直到现在还没有人工作。这是我的广播接收器。

public class StartBoot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Intent intent1 = new Intent(context,MyService.class);
            context.startService(intent1);
        }
    }
}

这是我的清单

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".StartBoot">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

在日志中我读到了这个

W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x9000010 (has extras) } to com.google.android.apps.docs/.app.NotificationChannelReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)

提前感谢您的回答

【问题讨论】:

  • 需要在manifest中添加BOOT权限
  • 我添加了它,但我忘记在请求中添加它,现在我已经更正了它

标签: android broadcastreceiver android-manifest android-permissions


【解决方案1】:

删除

<category android:name="android.intent.category.DEFAULT"/>

来自&lt;intent-filter&gt; 内的&lt;receiver&gt; 标记。只有在 &lt;activity&gt; 声明中的 &lt;intent-filter&gt; 才需要此类别。对于&lt;receiver&gt;,您只需要 ACTION。

您发布的错误消息与您的应用程序无关,它是在抱怨另一个应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多