android兑现简单定时关机应用程序

android实现简单定时关机应用程序

android兑现简单定时关机应用程序

先看一下简单的图形界面吧,比较简单的实现定时关机!!!(我的上传资源中有整个工程)

当然我们是在eclipse下开发的,那么我们先建一个工程Shutdown(当然可以随便取),然后就是包名,类名什么的。。。这就不多说了,这里先看一下上面那个时钟的

代码:analogClock=(AnalogClock)findViewById(R.id.anolag_clock);
        new Thread(){
            public void run() {
                 try {
                 while(true)
                  {    
                 Thread.sleep(1000);            
                 tick++;              
                 Message msg = Message.obtain();     
                                msg.arg1 = tick;   
                                handler.sendMessage(msg);                            
                  }                       
                 }                
                 catch (Exception e) {
                     e.printStackTrace();
                 }
                }               
        }.start();
        handler=new Handler(){
            public void handleMessage(Message msg) {
                Calendar calendar=Calendar.getInstance();
                int h=calendar.getTime().getHours();
                int m=calendar.getTime().getMinutes();
                        int hour=h, minute = m;                                   
                        tick=msg.arg1;
                        minute+= tick/60;
                        tick =tick%60;
                        hour+= minute /60;                             
                        minute=minute%60;
                        String str ="                                  NOW TIME          "+ MessageFormat   
                        .format("{0,number,0}:{1,number,00}", hour%24,minute);                      
                text.setText(str);
                                      
                super.handleMessage(msg);
            }
        };   
显示当前的时钟。

save button响应事件

 class button_saveListener implements OnClickListener
    {
       
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            if(edit_hour.getText().toString().equals("")||edit_minute.getText().toString().equals(""))
                Toast.makeText(ShutdownActivity.this, "have no shutdown time", Toast.LENGTH_SHORT).show();
            else
            {
            editor.putInt("hour",Integer.parseInt(edit_hour.getText().toString()));
            editor.putInt("minute",Integer.parseInt(edit_minute.getText().toString()));
            editor.commit();
            Toast.makeText(ShutdownActivity.this, "save success", Toast.LENGTH_SHORT).show();
            }
            text_time.setText("SHUTDOWN TIME   :"+Integer.parseInt(edit_hour.getText().toString())+":"+Integer.parseInt(edit_minute.getText().toString()));
                Intent intent =new Intent();
                intent.setClass(ShutdownActivity.this, ServiceActivity.class);//要建议个Service
                startService(intent);
        }        
    }

我们再来看看Service类:

public void onCreate() {

        SharedPreferences myshaPreferences=getSharedPreferences("time",Activity.MODE_PRIVATE);
        hour=myshaPreferences.getInt("hour", -1);
        minute=myshaPreferences.getInt("minute", -1);
        Thread thread=new Thread(){
            public void run()
            {    
                while(true)
                   {
                    Calendar calendar=Calendar.getInstance();                    
                    h=calendar.getTime().getHours();
                    m=calendar.getTime().getMinutes();
                    
                    if(h==hour&&m==minute)
                    {   
                        Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);//这里是会报错的,所以不能在eclipse下编译,要在源码下编译(下面会介绍)
                        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(newIntent);
                        break;
                    }
                    try {
                          Thread.sleep(1000);
                        } catch (Exception e)
                        {
                          e.printStackTrace();
                        }
                 }    
                }            
        };
        thread.start();    
        super.onCreate();
    }

在Manifast.xml文件中添加如下权限: <uses-permission android:name="android.permission.SHUTDOWN"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
还是贴出来吧:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jxnu.shutdown"
    android:versionCode="1"
    android:versionName="1.0"
    >
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.SHUTDOWN"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>"
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ShutdownActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".ServiceActivity" android:label="@string/app_name" android:enabled="true"></service>       
         <receiver android:name=".ShutdownBroadcastReceiver">
        <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">    
        </action>
        </intent-filter>
        </receiver>
        </application>
       
建立一个  BroadcastReceiver来接收广播android.intent.action.BOOT_COMPLETED(每次开机就启动service)

public class ShutdownBroadcastReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
         if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
             Intent newIntent = new Intent(context,ServiceActivity.class);
             context.startService(newIntent);
         }      
    }
}
大概差不多了,但是要将你的工程放到源码的package/app中,另外还得建一个Android.mk文件,然后就是编译了,生成的Shutdown.apk会在out/.../system/app下,可以拿来安装。


具体在我上传的资源中有整个工程,想要学习的同学可以借鉴一下.