Android-启动启动服务

从我在 StackExchange 和其他地方看到的所有东西中,我已经正确地设置好了在 Android 操作系统启动时启动意向服务的所有东西。不幸的是,它没有启动,我没有得到任何错误。也许专家能帮上忙。

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phx.batterylogger"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">


<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />


<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".BatteryLogger"/>
<receiver android:name=".StartupIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>


</manifest>

启动时的 BroadcastReceiver:

package com.phx.batterylogger;


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


public class StartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, BatteryLogger.class);
context.startService(serviceIntent);
}
}

UPDATE : 我尝试了下面的所有建议,并将诸如 Log.v("BatteryLogger", "Got to onReceive, about to start service");之类的日志记录添加到 StartupInentReceiver 的 onReceive 处理程序中,没有任何记录。所以它甚至没有到达广播接收器。

我认为我正在正确地部署和测试 APK,只是在 Eclipse 中运行 Debug,控制台说它成功地将它安装到我的 Xoom 平板电脑 BatteryLogger bin BatteryLogger.APK 上。然后,为了进行测试,我重新启动平板电脑,然后查看 DDMS 中的日志,并检查操作系统设置中的 RuningServices。这一切听起来正确吗,还是我遗漏了什么?再次感谢您的帮助。

135662 次浏览

看起来非常类似于 我的,但我使用接收器的完整包名称:

<receiver android:name=".StartupIntentReceiver">

我有:

<receiver android:name="com.your.package.AutoStart">

我在没有全套服务的情况下取得了成功,你知道呼叫链在哪里被打断了吗?如果使用 Log()进行调试,在什么时候它不再工作?

我想可能是在你的意向服务,这一切看起来很好。

这里是一个完整的自动启动应用程序的例子

AndroidManifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0">


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


<application android:icon="@drawable/icon" android:label="@string/app_name">


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


<activity android:name=".hello"></activity>
<service android:enabled="true" android:name=".service" />
</application>
</manifest>

Autostart.java

public class autostart extends BroadcastReceiver
{
public void onReceive(Context context, Intent arg1)
{
Intent intent = new Intent(context,service.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
Log.i("Autostart", "started");
}
}

Service.java

public class service extends Service
{
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}


@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),hello.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}

Java -在执行一次 Applicaton 之后,每次启动设备时都会弹出这个命令。

public class hello extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
}
}

下面应该可以。我已经确认过了。也许你的问题在别的地方。

接收者:

public class MyReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
Log.d("TAG", "MyReceiver");
Intent serviceIntent = new Intent(context, Test1Service.class);
context.startService(serviceIntent);
}
}
}

服务范围:

public class Test1Service extends Service {
/** Called when the activity is first created. */
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "Service created.");
}
    

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "Service started.");
return super.onStartCommand(intent, flags, startId);
}
    

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d("TAG", "Service started.");
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="8" />


<application android:icon="@drawable/icon" android:label="@string/app_name">
    

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS"
/>
<!--        <activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity> -->
<service android:name=".Test1Service"
android:label="@string/app_name"
>
</service>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>

由于设备在引导后进入睡眠状态,您的服务可能在完成之前被关闭。您需要先获得一个唤醒锁。幸运的是,支持库为我们提供了一个类做到了这一点:

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// This is the Intent to deliver to our service.
Intent service = new Intent(context, SimpleWakefulService.class);


// Start the service, keeping the device awake while it is launching.
Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
startWakefulService(context, service);
}
}

然后,在你的服务中,确保释放唤醒锁:

    @Override
protected void onHandleIntent(Intent intent) {
// At this point SimpleWakefulReceiver is still holding a wake lock
// for us.  We can do whatever we need to here and then tell it that
// it can release the wakelock.


...
Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
SimpleWakefulReceiver.completeWakefulIntent(intent);
}

不要忘记添加 WAKE _ LOCK 权限:

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

只是为了使搜索更容易,正如在评论中提到的,< strike > 这是不可能的,因为3.1 Https://stackoverflow.com/a/19856367/6505257

我已经找到了一种方法,使您的应用程序运行良好时,设备重新启动,请按照下面的步骤成功。

AndroidManifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


<application android:icon="@drawable/icon" android:label="@string/app_name">


<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=".UIBootReceiver" android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".class_Service" />
</application>
</manifest>

UIBootReceiver

public class UIBootReceiver extends BroadcastReceiver {


private static final String TAG = "UIBootReceiver";


@Override
public void onReceive(Context context, Intent arg1)
{
Toast.makeText(context, "started", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,class_Service.class);
context.startService(intent);
}
}

这是请求许可,不需要管理这个应用程序的电池节省,所以你可以在后台运行稳定。

在 MainActivity 类的 onCreate ()中声明这段代码:

    Intent myIntent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
myIntent.setData(Uri.parse("package:" +
DeviceMovingSpeed.this.getPackageName()));
}
startActivity(myIntent);