gpt4 book ai didi

Android 应用程序在设备启动时崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 09:02:36 24 4
gpt4 key购买 nike

我正在开发一个需要在 Android 启动时启动 BackgroundService 的应用程序

这是我用来在 Android 启动时启动 BroadcastReceiver 的代码

public class StartOnBootService extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
try
{
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.package.myApplicationPackage.BackgroundService");
context.startService(serviceIntent);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

这是我的BackgroundService.class

public class BackgroundService extends Service {

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

@Override
public void onCreate() {
//code to execute when the service is first created
Toast.makeText(getBaseContext(), "BACKGROUND SERVICE STARTED", Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
//code to execute when the service is shutting down
}

@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}

这是我在 Android 启动时从 CatLog 中截取的错误日志。

12-21 10:28:01.279: E/EmbeddedLogger(1710): App crashed! Process: com.package.myApplicationPackage
12-21 10:28:01.289: E/EmbeddedLogger(1710): App crashed! Package: com.package.myApplicationPackage v1 (1.0)
12-21 10:28:01.289: E/EmbeddedLogger(1710): Application Label: myApp Label

我的AndroidManifest.xml文件

        </service>
<service android:name=".BackgroundService">

<intent-filter>
<action android:name="com.package.myApplicationPackage.BackgroundService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartOnBootService"
android:enabled="true"
android:exported="true"
android:label="StartOnBootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

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

最佳答案

您的广播接收器名称和 android list 接收器名称完全不同。

StartOnBootService 或者在 list 中是 StartMyServiceAtBootReceiver

关于Android 应用程序在设备启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com