开始我的服务? [英] Starting my service?

查看:94
本文介绍了开始我的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个广播接收器应该在设备启动时调用(它不是),并且它开始我的服务。不幸的是,该服务还没有启动!

I have a broadcast reciever that should get called whenever the device boots up (it isn't) , and it starts my service. Unfortunately, the service isn't starting!

我看过这个页面,阅读每个答案,并遵循每一步......但它仍然无效。我想在手机重启/开机时启动我的服务。

I have looked at this page, read every answer, and followed every step...but it is still not working. I want to start my service whenever the phone restarts/powers on.

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



    }
}

但我的服务仍然没有开始。这是我的清单:

But my service still wont start. Here is my manifest:

<?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>

过去三天我一直坚持这个,和我真的很感谢你的帮助。为什么我的服务没有在启动时启动?

I have been stuck on this for the last three days, and I would really appreciate your help. Why is my service not starting on boot?

推荐答案

以下是我定义自动启动接收器的方法:

Here's how I define my auto start receiver:

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

注意 android.intent.action.BOOT_COMPLETED

这篇关于开始我的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆