代码 –

public class ShutdownReceiver extends BroadcastReceiver {

private static final String TAG = "ShutdownReceiver";

@Override

public void onReceive(final Context context, final Intent intent) {

Logger.i(TAG, "Shutting Down..........................");

if("android.intent.action.ACTION_SHUTDOWN".equals(intent.getAction())) {

//Power Off

}

}

}

服务 –

private BroadcastReceiver mReceiver = null;

@Override

public void onCreate() {

super.onCreate();

// INITIALIZE RECEIVER

//It is used to register broadcast for ShutDown or Power Off

IntentFilter filter = new IntentFilter(Intent.ACTION_SHUTDOWN);

mReceiver = new ShutdownReceiver();

registerReceiver(mReceiver, filter);

}

@Override

public void onDestroy() {

unregisterReceiver(mReceiver);

}

权限 –

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐