萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 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>     
<rec
copyright © 萬盛學電腦網 all rights reserved