开机后自动关机的功能

首先定义一个什么事都不做的Activity,作为应用的启动Activity.这个Activity一定要定义的,要不然接收不到Broadcast的。

再来就是定义一个Broadcast类,可以收到android.intent.action.BOOT_COMPLETED广播的。这个类在收到android.intent.action.BOOT_COMPLETED广播后,5s后关机。

代码如下:

public class AlarmShutdownReceiver extends BroadcastReceiver {
    static final String ACTION_COMP = "android.intent.action.BOOT_COMPLETED";
     static final String TAG = "AlarmShutdownReceiver";
     private Timer mTimer = null;
     private int mfristtime = 0;
      @Override
    public void onReceive(Context context, Intent intent) {
          final Context  icontext = context;
        Log.v(TAG,"onReceive+");
        
        if(intent.getAction().equals(ACTION_COMP))
         {     
            Log.v(TAG,"boot completed+");
            Toast.makeText(context, "shutdown service start, waitting 5s to shutdown", Toast.LENGTH_SHORT).show();  
            mTimer = new Timer();          
            mTimer.schedule(new TimerTask() {              
                        @Override  
                        public void run() {   
                           mfristtime++;
                           if(1 == mfristtime)
                                {  
                               Log.v(TAG,"send shutdown intent +");
                               Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); 
                               shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
                               shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                               icontext.startActivity(shutdown);
                               Log.v(TAG,"send shutdown intent +");
                                }
                        }   
                    }, 5*1000, 5*1000);   
           Log.v(TAG,"boot completed-");
         }
         Log.v(TAG,"onReceive-");
    }
}
Intent.ACTION_REQUEST_SHUTDOWN;
 Intent.EXTRA_KEY_CONFIRM, false;

这两个属性应该是系统级别的属性,对外是不公开的,在android 源码下是可以编译通过的。

AndroidManifest.xml定义如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="XXXX"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system">

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.SHUTDOWN" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <receiver  android:name=".AlarmShutdownReceiver"
              android:enabled="true"  
              android:exported="true"
           >      
         <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>             
        </intent-filter>           
        </receiver>  
        <activity
            android:name=".AutoShutdown"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
另外Android.mk文件定义要包含以下语句:

LOCAL_CERTIFICATE  := platform

mm  编译就可以了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值