BOOT_COMPLTED 上的 AutoStart 应用程序无法启动服务 Intent not found
AutoStart application on BOOT_COMPLTED unable to start service Intent not found
我正在尝试创建一个没有 UI/Activity 的服务应用程序。
该服务将于 BOOT_COMPLETED 开始。目前我遇到接收方服务无法启动主服务的问题。
错误听起来像这样(Android 设备监视器):
标签:Activity经理
文本:无法启动服务 Intent { cmp=com.remote.cat/.ActionService } U=0:未找到
我的 android OS 设备上的版本是 4.2.2
我正在 PowerShell 中通过此命令对其进行测试:
adb.exe shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.remote.cat/.AutoStartServiceReceiver
这两个服务都在包的根目录下com.remote.cat
感觉好像遗漏了一些小东西或打字错误,非常感谢任何帮助!
谢谢!
清单代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.remote.cat"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<receiver android:name="com.remote.cat.AutoStartServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<service android:name="com.remote.cat.AutoStartServiceReceiver"></service>
<service android:name="com.remote.cat.ActionService"></service>
</manifest>
这里是 brodcastreceiver class:
package com.remote.cat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStartServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent serviceIntent = new Intent(context, ActionService.class);
//Intent serviceIntent = new Intent("com.remote.cat.ActionService");
context.startService(serviceIntent);
}
}
}
这是我要启动的主要服务:
package com.remote.cat;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ActionService extends Service
{
private ScheduledThreadPoolExecutor executor = null;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
executor = new ScheduledThreadPoolExecutor(4);
final ScheduledFuture<?> handle = executor.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "one minute message", Toast.LENGTH_LONG).show();
}
}, 0, 8, TimeUnit.SECONDS);
return START_STICKY;
}
}
将清单中的 AutoStartServiceReceiver
声明更改为 <receiver>
而不是 <service>
。
错误在你的广播接收器class
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
应该是
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
编辑
我认为您必须在应用程序标签内声明您的服务,因为它是应用程序的一部分
我正在尝试创建一个没有 UI/Activity 的服务应用程序。 该服务将于 BOOT_COMPLETED 开始。目前我遇到接收方服务无法启动主服务的问题。
错误听起来像这样(Android 设备监视器):
标签:Activity经理 文本:无法启动服务 Intent { cmp=com.remote.cat/.ActionService } U=0:未找到
我的 android OS 设备上的版本是 4.2.2
我正在 PowerShell 中通过此命令对其进行测试:
adb.exe shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.remote.cat/.AutoStartServiceReceiver
这两个服务都在包的根目录下com.remote.cat
感觉好像遗漏了一些小东西或打字错误,非常感谢任何帮助! 谢谢!
清单代码如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.remote.cat"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<receiver android:name="com.remote.cat.AutoStartServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<service android:name="com.remote.cat.AutoStartServiceReceiver"></service>
<service android:name="com.remote.cat.ActionService"></service>
</manifest>
这里是 brodcastreceiver class:
package com.remote.cat;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStartServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent serviceIntent = new Intent(context, ActionService.class);
//Intent serviceIntent = new Intent("com.remote.cat.ActionService");
context.startService(serviceIntent);
}
}
}
这是我要启动的主要服务:
package com.remote.cat;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ActionService extends Service
{
private ScheduledThreadPoolExecutor executor = null;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
executor = new ScheduledThreadPoolExecutor(4);
final ScheduledFuture<?> handle = executor.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "one minute message", Toast.LENGTH_LONG).show();
}
}, 0, 8, TimeUnit.SECONDS);
return START_STICKY;
}
}
将清单中的 AutoStartServiceReceiver
声明更改为 <receiver>
而不是 <service>
。
错误在你的广播接收器class
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
应该是
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
编辑
我认为您必须在应用程序标签内声明您的服务,因为它是应用程序的一部分