BackgroundService
Android Boot で起動する必要があるアプリケーションに取り組んでいます
BroadcastReceiver
これは、 Android Bootで起動するために使用しているコードです
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" />