操作系统启动时启动后台服务

时间:2016-08-10 17:40:08

标签: java android background-service

我试图在设备启动时启动后台服务,但什么也没发生。这是我的代码:

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.test"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>
    </application>
</manifest>

Autostart.java

package it.test;

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

public class Autostart extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Intent intent = new Intent(arg0, AppService.class);
        arg0.startService(intent);
        Log.e("it.Test", "***** SERVER STARTING CALLED *****");

    }
}

AppService.java

package it.test;

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

public class AppService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My AppService Stopped", Toast.LENGTH_LONG).show();
        Log.e("it.Test", "***** SERVICE STOPPED *****");
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
        Log.e("it.Test", "***** SERVICE STARTED *****");
        /*
        MainActivity.calc();
        MainActivity.save();
        */
    }
}

当我的手机开机时,我看不到任何祝酒词(我没有看到&#34;我的AppService已启动&#34;也没有&#34;我的AppService已停止&#34;吐司)并且应用程序没有记录任何内容...... 谁知道我做错了什么? 谢谢!

编辑:解决问题的方法 看到帖子Trying to start a service on boot on Android在答案中链接后,经过多次调试后我找到了解决问题的方法:)谢谢大家!

对AndroidManifest.xml的修改

<application>内添加:

<receiver android:name="it.test.Autostart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="it.test.AppService"
    android:enabled="true" />

还添加:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    [...]
    android:installLocation="internalOnly">

对AppService.java的修改 为了使服务有效,我必须在AppService类onStartCommand而不是onStart中覆盖,因此我从类中完全删除了方法onStart。 新onStartCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "My AppService Started", Toast.LENGTH_LONG).show();
    Log.e("it.Test", "***** SERVICE STARTED ***** from onStartCommand");
    MainActivity.calc(this);
    MainActivity.save();
    return Service.START_REDELIVER_INTENT;//todo I'm not sure about what I have to return
}

5 个答案:

答案 0 :(得分:2)

您的广播接收器未在AndroidManifest.xml注册

您需要注册广播接收器:

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

如果您的应用已安装到外部存储设备,则无法收到BOOT_COMPLETE广播消息。为防止这种情况,您可以在内部存储中安装应用程序。你可以在AndroidManifest.xml中添加这一行

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
... >

来源:https://stackoverflow.com/a/32938071/1549700

答案 1 :(得分:1)

您是否在<application>标签

下注册了广播接收器和服务
 <!-- [START service_listener] -->
    <service
        android:name=".it.test.AppService"
        android:enabled="true" />
    <!-- [END service_listener] -->

    <!-- [START broadcast_receiver] -->
    <receiver android:name=".it.test.Autostart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <!-- [END broadcast_receiver] -->

答案 2 :(得分:0)

您需要注册广播接收器:

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

答案 3 :(得分:0)

您需要在清单文件中使用此功能:

<receiver android:name=".it.test.Autostart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

答案 4 :(得分:0)

  

您是否已在清单文件中注册广播接收器,并且还在广播接收器中提供启动完成许可,如果有任何问题,请在评论中提问,您的代码是完美的。