开机后自动关机的功能 .

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

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

代码如下:

 
public class AlarmShutdownReceiver extends BroadcastReceiver {  
  1.     static final String ACTION_COMP = "android.intent.action.BOOT_COMPLETED";  
  2.      static final String TAG = "AlarmShutdownReceiver";  
  3.      private Timer mTimer = null;  
  4.      private int mfristtime = 0;  
  5.       @Override  
  6.     public void onReceive(Context context, Intent intent) {  
  7.           final Context  icontext = context;  
  8.         Log.v(TAG,"onReceive+");  
  9.           
  10.         if(intent.getAction().equals(ACTION_COMP))  
  11.          {       
  12.             Log.v(TAG,"boot completed+");  
  13.             Toast.makeText(context, "shutdown service start, waitting 5s to shutdown", Toast.LENGTH_SHORT).show();    
  14.             mTimer = new Timer();            
  15.             mTimer.schedule(new TimerTask() {                
  16.                         @Override    
  17.                         public void run() {     
  18.                            mfristtime++;  
  19.                            if(1 == mfristtime)  
  20.                                 {    
  21.                                Log.v(TAG,"send shutdown intent +");  
  22.                                Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);   
  23.                                shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, false);  
  24.                                shutdown.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  25.                                icontext.startActivity(shutdown);  
  26.                                Log.v(TAG,"send shutdown intent +");  
  27.                                 }  
  28.                         }     
  29.                     }, 5*1000, 5*1000);     
  30.            Log.v(TAG,"boot completed-");  
  31.          }  
  32.          Log.v(TAG,"onReceive-");  
  33.     }  
  34. }  
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定义如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="XXXX"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0"  
  6.     android:sharedUserId="android.uid.system">  
  7.   
  8.     <uses-sdk android:minSdkVersion="15" />  
  9.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
  10.     <uses-permission android:name="android.permission.SHUTDOWN" />  
  11.       
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name" >  
  15.           
  16.         <receiver  android:name=".AlarmShutdownReceiver"  
  17.               android:enabled="true"    
  18.               android:exported="true"  
  19.            >        
  20.          <intent-filter>  
  21.                 <action android:name="android.intent.action.BOOT_COMPLETED"/>               
  22.         </intent-filter>             
  23.         </receiver>    
  24.         <activity  
  25.             android:name=".AutoShutdown"  
  26.             android:label="@string/app_name" >  
  27.             <intent-filter>  
  28.                 <action android:name="android.intent.action.MAIN" />  
  29.   
  30.                 <category android:name="android.intent.category.LAUNCHER" />  
  31.             </intent-filter>  
  32.         </activity>  
  33.     </application>  
  34.   
  35. </manifest>  
<?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文件定义要包含以下语句:

  1. LOCAL_CERTIFICATE  :platform  
LOCAL_CERTIFICATE  := platform

mm  编译就可以了。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值