我想开发简单的应用程序(服务),它将在启动时自动启动。主要活动也创建了新的服务(我可以在服务任务管理器列表中看到它)。这个服务也应该在重新启动我的手机之后创建(三星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的清单标签中。感谢您的建议。 –