我想開發簡單的應用程序(服務),它將在啓動時自動啓動。主要活動也創建了新的服務(我可以在服務任務管理器列表中看到它)。這個服務也應該在重新啓動我的手機之後創建(三星Galaxy Ace與android 2.3.4)而不啓動應用程序,但它不是 - 我不能在服務任務管理器列表中看到它。哪裏可以解決問題?這裏是我的代碼:自動啓動在Android應用服務
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.nafik.testService"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestServiceActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:icon="@drawable/ic_launcher"
android:label="ledNotifier"
android:enabled="true">
<intent-filter>
<action android:name="cz.nafik.testService.MyService" />
</intent-filter>
</service>
<receiver android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MyService.java
package cz.nafik.testService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "service bind", Toast.LENGTH_LONG).show();
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "service destroyed", Toast.LENGTH_LONG).show();
}
}
BootUpReceiver.java
package cz.nafik.testService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setAction("cz.nafik.testService.MyService");
context.startService(intent);
}
}
TestServiceActivity.java
package cz.nafik.testService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class TestServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent serviceIntent = new Intent();
serviceIntent.setAction("cz.nafik.testService.MyService");
this.startService(serviceIntent);
}
}
我的手機已經紮根,OS可以自動安裝新的應用程序到SD卡。這是我的問題,所以我必須將android:installLocation =「internalOnly」放入AndroidManifest.xml的清單標籤中。感謝您的建議。 –