Android implements simple Timed Shutdown of applications

Source: Internet
Author: User

 

Let's take a look at the simple graphic interface. It's easy to implement Timed Shutdown !!! (The entire project is included in my upload resources.) Of course, we developed it in eclipse, so we should first create a project Shutdown (of course we can take it as needed), and then we will name the package, class Name or something... Let's not talk about it. Here we will first look at the code of the above clock: 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 );
}
};
Displays the current clock. Save button RESPONSE event 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); // recommended Service
StartService (intent );
}
} Let's take a look at the Service class: 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); // an error is reported here, so it cannot be compiled in eclipse. It must be compiled in the source code (which will be introduced below)
NewIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
StartActivity (newIntent );
Break;
}
Try {
Thread. sleep (1000 );
} Catch (Exception e)
{
E. printStackTrace ();
}
}
}
};
Thread. start ();
Super. onCreate ();
} Add the following permissions to the Manifast. xml file: <uses-permission android: name = "android. permission. SHUTDOWN"/>
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>"
Or paste it: <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>
<Cycler android: name = ". ShutdownBroadcastReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED">
</Action>
</Intent-filter>
</Cycler>
</Application>

Create a BroadcastReceiver to receive broadcast android. intent. action. BOOT_COMPLETED (start service every time you start the system) 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 );
}
}
}
It's almost the same, but you need to put your project in the package/appof the source code, and create an android.mkfile. then compile and compile the file, and the generated shutdown.apk will be installed in the out/.../system/app.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.