1
A
答えて
5
まず、あなたはあなたのマニフェストに許可が必要です:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
また、あなたのマニフェストでは、あなたのサービスを定義し、ブート完了したアクションのために聞く:
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
は、その後必要にBOOT_COMPLETEDアクションを取得してサービスを開始する受信者を定義します。
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("com.myapp.MySystemService");
context.startService(serviceIntent);
}
}
}
これで、電話機の起動時にサービスが実行されているはずです。ここで
0
は、
のAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.practice" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AutoStartExampleActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="AutoStart"></receiver>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
AutoStartExampleActivityファイル
package com.practice;
import android.app.Activity;
import android.os.Bundle;
public class AutoStartExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
BroadcastReceiverコード、
package com.practice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStart extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
System.out.println ("Application Started");
// put your TimerTask calling class here
try
{
Intent myIntent = new Intent (context, AutoStartExampleActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
catch (Exception e)
{
System.out.println (" Error while Starting Activity " + e.toString());
}
}
}
サンプルコードを作業の完了です
関連する問題
- 1. 起動時に再起動アプリケーションを起動する
- 2. アプリケーション起動時のSwift起動アプリ
- 3. アプリケーション起動時に起動するQTMovieファイルを保存する
- 4. 起動時にChromeアプリケーションを起動する
- 5. 起動時にplackアプリケーションを起動する方法
- 6. Windowsの起動時にアプリケーションを起動する
- 7. iPhone起動時にアプリケーションを起動する方法
- 8. 起動時にコードネーム1のアプリケーションを起動する
- 9. 起動時にlaravelアプリケーションを起動する
- 10. サーバーの起動時にアプリケーションを起動する方法
- 11. Windows 7起動時にプログラムを起動する(自動起動)
- 12. 起動時にiOSを起動する
- 13. 起動時に起動しない起動時にnginxを起動しない
- 14. 起動時ドッカーマシン起動時
- 15. アプリケーション起動時にHSQLDBデータベースマネージャを起動しますか?
- 16. C#の起動時にアプリケーションを起動します
- 17. didReceiveLocalNotificationは常にアプリケーション起動時に起動します
- 18. Azure Appサービス - 起動時に跳ね返るSpring起動アプリケーション
- 19. 起動時にアプリケーションを挿入する
- 20. RNのシステム起動時に自動的にアプリケーションを起動する
- 21. Android 2.2:起動時の自動起動アプリケーション問題
- 22. 起動時にiOSアプリケーションがクラッシュする
- 23. 起動時にアプリケーションがクラッシュする
- 24. iPhoneアプリケーションが起動時にクラッシュする
- 25. 起動時にアプリケーションがクラッシュする
- 26. Android:Bluetoothアプリケーションが起動時にクラッシュする
- 27. 起動時にアプリケーションがクラッシュする
- 28. WiX:Windowsの起動時に起動するアプリケーションを登録するには?
- 29. 起動時のMacのアプリケーション
- 30. log4j - アプリケーション起動時のトリガーログローリング