Tạo một dịch vụ mà nhận được phát sóng cho tắt máy như thế này:
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 ++");
}
}
Trong AndroidManifest.xml
của bạn, nhúng đoạn dưới đây trong <application>
tags:
<receiver android:name=".MyAppShutdown">
<intent-filter>
<action android:name="android.intent.action.SHUTDOWN"/>
</intent-filter>
</receiver>
Từ hoạt động của ứng dụng của bạn đăng ký một bộ thu phát sóng có bộ lọc thích hợp cho các mục đích:
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();
}
}
}
}
Oklets giả sử tôi gọi LogOff(). Khi nền tảng Radio mạng tắt, tôi không thể gửi dữ liệu !!! – NitZRobotKoder
Vâng! Tôi đã phần nào nhận ra sau khi đọc lại câu hỏi của bạn một lần nữa ... ngạc nhiên, không thể tin rằng android đã thay đổi nó, có thể nghĩ radio là thứ cuối cùng bị đóng cửa - xin lỗi vì bài đăng rambling vì nó không phải là thứ bạn đang tìm kiếm cho ... – t0mm13b
thats Ok !! Yeah lạ lý do tại sao các hành vi đã được thay đổi !! – NitZRobotKoder