【问题标题】:Trouble starting my Android service on boot启动时无法启动我的 Android 服务
【发布时间】:2016-02-12 22:48:19
【问题描述】:

我无法在启动时启动我的服务。

我有一个广播接收器,只要设备启动(它不是)就应该调用它,它会启动我的服务。不幸的是,服务没有启动!

我查看了this 页面,阅读了每个答案,并遵循了每个步骤......但它仍然无法正常工作。我想在手机重启/开机时启动我的服务。


package curlybrace.ruchir.myApp;

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

public class BootUp extends BroadcastReceiver {

    private static final String TAG = "myTag";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        Log.v(TAG, "Hooray! Received boot! :) "); //Sadly I am not getting this message

        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);



    }
}

但我的服务仍然无法启动。这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="curlybrace.ruchir.myApp"
    android:installLocation="internalOnly">

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

    <uses-permission android:name="android.permission.VIBRATE"/>

    <uses-permission android:name="android.permission.SEND_SMS"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Settings"
            android:label="@string/title_activity_settings"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

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




        <receiver
            android:name=".BootUp"
            >

            <intent-filter>
                <action android:name="android.intent.action.RECIEVE_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>

        </receiver>
    </application>

</manifest>

过去三天我一直坚持这一点,非常感谢您的帮助。为什么我的服务没有在启动时启动?

【问题讨论】:

  • 只是为了检查-您至少在应用中启动了一次活动,对吗? (如果没有,apk处于停止状态,不会接收广播
  • @GabeSechan 是的,我已经打开了应用程序

标签: java android


【解决方案1】:

我是这样定义我的自动启动接收器的:

<receiver android:name=".receivers.AutoStartReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

注意android.intent.action.BOOT_COMPLETED

【讨论】:

  • 所以我删除接收?
  • @RuchirBaronia 是的。用我粘贴的内容替换整个 &lt;intent-filter&gt; 部分,它应该可以正常工作。
  • 那么recieve boot completed是做什么的?
  • @RuchirBaronia 我不确定(谷歌搜索)但没关系,因为 'android.intent.action.BOOT_COMPLETED' 是正确的操作:-D
  • 好的,谢谢。这将在重新启动和关机时启动我的服务 - >开机,对吗?这会在其他任何时候被调用吗?谢谢! :)
【解决方案2】:

您的BroadcastReceiver 不会被调用,因为广播的操作不是android.intent.action.BOOT_COMPLETED 而不是RECIEVE_BOOT_COMPLETED。

如果你修复了你的接收器应该可以工作。

您启动服务的代码看起来不错。

【讨论】:

    【解决方案3】:
    android:installLocation="internalOnly"
    

    【讨论】:

    • 请编辑更多信息。不建议使用纯代码和“试试这个”的答案,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多