有人知道我启动Android手机时无法启动服务的原因吗?我正在尝试将我的服务链接到我的活动类,当活动崩溃时,服务将再次重新启动活动。我的服务类如下
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;
}
} }
我的清单如下
<?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>
有没有人知道为什么在启动时不会调用它?谢谢!
答案 0 :(得分:0)
在清单文件中,您以错误的方式声明了元素。
<?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(服务类的意图)方法