android實現簡單定時關機應用程式

來源:互聯網
上載者:User

 

先看一下簡單的圖形介面吧,比較簡單的實現定時關機!!!(我的上傳資源中有整個工程)當然我們是在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下,可以拿來安裝。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.