「android入門」androidでServiceまたはappが自動起動するプログラム

1.onReceiveメソッド
コード下記:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
publicclass BootBroadcastReceiver extends BroadcastReceiver {
//onReceiveメソッドを書き直す
@Override
publicvoid onReceive(Context context, Intent intent) {
//XXX.classが起動サービス
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v(“TAG", “自動起動…..");
//アプリケーションが起動
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
}
}

2.xmlファイルを設定
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>

3.権限を追加
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Android

Posted by arkgame