次のように、シャットダウンのブロードキャストを受信するサービスを作成します。
public class MyAppShutdown extends BroadcastReceiver{
private static final String TAG = "MyAppShutdown";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v(TAG, "onReceive - ++ ENTER ++");
if (intent != null && intent.getAction().equals(Intent.ACTION_SHUTDOWN)){
Log.v(TAG, "onReceive - ***] SHUTDOWN [***");
// perhaps send a broadcast to your app via intent to perform a log out.
Intent intent = new Intent();
intent.addAction("intent.myapp.action.shutdown");
sendBroadcast(intent);
}
Log.v(TAG, "onReceive - ++ LEAVE ++");
}
}
で、このフラグメントを以下のタグAndroidManifest.xml
内に埋め込みます。<application>
<receiver android:name=".MyAppShutdown">
<intent-filter>
<action android:name="android.intent.action.SHUTDOWN"/>
</intent-filter>
</receiver>
アプリのアクティビティから、インテント用の適切なフィルターを使用してブロードキャスト レシーバーを登録します。
public class myApp extends Activity{
private myAppBroadcastRcvr myAppRcvr = new myAppBroadcastRcvr();
@Override
public void onCreate(Bundle savedInstanceState){
IntentFilter filter = new IntentFilter();
filter.addAction("intent.myapp.action.shutdown");
registerReceiver(myAppRcvr, filter);
}
// Perhaps you have this
private void LogOff(){
}
class myAppBroadcastRcvr extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("intent.myapp.action.shutdown")){
LogOff();
}
}
}
}