起動時にサービスを開始する必要があります。いろいろ探しました。彼らはBroadcastreceiverについて話しています。私は Android 開発に慣れていないので、Android のサービスについて明確なイメージがありませんでした。いくつかのソースコードを提供してください。
9 に答える
あなたの受信機:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, YourService.class);
context.startService(myIntent);
}
}
あなたの AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast.receiver.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".BR_Example"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declaring broadcast receiver for BOOT_COMPLETED event. -->
<receiver android:name=".MyReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<!-- Adding the permission -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
を作成しBroadcastReceiver
て登録し、ACTION_BOOT_COMPLETEDを受け取ります。RECEIVE_BOOT_COMPLETED権限も必要です。
デバイスの起動時に自動的に起動する独自のアプリケーション サービスを登録することができます。これは、たとえば、http サーバーからプッシュ イベントを受け取り、新しいイベントが発生したらすぐにユーザーに通知したい場合に必要です。ユーザーは、サービスが開始される前にアクティビティを手動で開始する必要はありません...
とても簡単です。まず、アプリにパーミッション RECEIVE_BOOT_COMPLETED を付与します。次に、BroadcastReveiver を登録する必要があります。これを BootCompletedIntentReceiver と呼びます。
Manifest.xml は次のようになります。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application> <receiver android:name=".BootCompletedIntentReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service android:name=".BackgroundService"/> </application> </manifest>
最後のステップとして、Receiver を実装する必要があります。このレシーバーは、バックグラウンド サービスを開始するだけです。
package com.jjoe64; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import com.jjoe64.BackgroundService; public class BootCompletedIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent pushIntent = new Intent(context, BackgroundService.class); context.startService(pushIntent); } } }
http://www.jjoe64.com/2011/06/autostart-service-on-device-boot.htmlから
BOOT_COMPLETE と REBOOT に登録する必要があります
<receiver android:name=".Services.BootComplete">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
まず、manifest.xml ファイルにレシーバーを登録します。
<receiver android:name="com.mileagelog.service.Broadcast_PowerUp" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
次に、このレシーバーのブロードキャストを次のように記述します。
public class Broadcast_PowerUp extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(context, "Service_PowerUp Started",
Toast.LENGTH_LONG).show();
} else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(context, "Service_PowerUp Stoped", Toast.LENGTH_LONG)
.show();
}
}
}