gpt4 book ai didi

android - 为什么我的服务在 Bootup 期间没有启动?

转载 作者:行者123 更新时间:2023-12-03 17:41:46 25 4
gpt4 key购买 nike

有谁知道为什么当我启动我的 Android 手机时我的服务无法启动?我正在尝试将我的服务链接到我的 Activity 类,当 Activity 崩溃时,服务将再次重新启动 Activity 。我的服务类如下

public class Restarter extends Service {

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


@Override
public void onCreate() {
super.onCreate();
Log.v("Restarter", "Restarter Created");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Stamp_Client sc = new Stamp_Client();
System.out.println("Restarter Service Started");
while(true){
System.out.println("Checking for crash...");
int instance;
instance = sc.nbInstances();
if(instance == 0){
Intent myIntent =
new Intent(Restarter.this,Stamp_Client.class);
Restarter.this.startActivity(myIntent);
}
try {
Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();
}

// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

} }

我的 list 如下
    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2r.cas.mofa.main"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Stamp_Client"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:configChanges="orientation|keyboardHidden">


<service android:enabled="true" android:name=".Restarter" >
<intent-filter>
<action android:name="com.i2r.cas.mofa.main.Restarter">
</action>
</intent-filter>
</service>


<receiver android:name=".StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

有谁知道为什么在启动过程中不会调用它?谢谢!

最佳答案

在您的 list 文件中,您以错误的方式声明了元素。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.i2r.cas.mofa.main"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Stamp_Client"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:enabled="true" android:name=".Restarter" >
<intent-filter>
<action android:name="com.i2r.cas.mofa.main.Restarter">
</action>
</intent-filter>
</service>
<receiver android:name=".StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>

在您的 StartBootServiceReceiver调用 startService(服务类的 Intent ) 方法

关于android - 为什么我的服务在 Bootup 期间没有启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7265871/

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