I searched the internet today to start the application. There are many examples, and I found that some real machines still do not work, so I have to do it on the die-like device. Paste some code I have tried
The following figure shows the main Activity. When it is opened, it displays the system time.
Package com. test;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import android. app. Activity;
Import android. app. KeyguardManager;
Import android. app. KeyguardManager. KeyguardLock;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class AtestActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Setcontentview (R. layout. Main );
TV = (textview) findviewbyid (R. Id. TV _test );
Date = new date ();
Simpledateformat simp = new simpledateformat ("yyyy-mm-dd hh-mm-SS ");
String time = simp. Format (date );
TV. settext (time );
TV. settextsize (25 );
TV. settextcolor (color. Red );
Keyguardmanager
= (KeyguardManager) getSystemService (KEYGUARD_SERVICE );
KeyguardLock lock = keyguardManager. newKeyguardLock (KEYGUARD_SERVICE );
Lock. disableKeyguard (); // disable the display of the keyboard lock. If the keyboard lock is currently displayed, hide it.
}
}
The following is the class inherited from BroadCastReceiver.
Package
Com. test;
Import android. app. AlarmManager;
Import android. app. PendingIntent;
Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. OS. SystemClock;
Import android. util. Log;
Public class StartListenerActivity extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent ){
Log. I ("receiver", "onReceiver begin ");
// This is a method used by someone on the Internet. I tried it on a real machine. It cannot be used. Maybe my mobile phone is blocked. Maybe this method is still available, but I think it is troublesome to use it.
// PendingIntent pendingIntent = PendingIntent // This is not necessarily the case and cannot be predicted
//. GetActivity (context, 0, new Intent (context, AtestActivity. class), PendingIntent. FLAG_UPDATE_CURRENT );
//
// Long time = SystemClock. elapsedRealtime ();
// AlarmManager alarmManager = (AlarmManager) context. getSystemService (Context. ALARM_SERVICE );
// This method is used by someone on the Internet only once.
// AlarmManager. set (AlarmManager. ELAPSED_REALTIME_WAKEUP, time, pendingIntent );
// Check once every 5 seconds,
// AlarmManager. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP, time, 5*1000, pendingIntent );
// Configure //-----------------------------------------------------------------------------------
// This is another method. I cannot use it on a real machine. I have not tried it on the simulator.
// String category = "android. intent. category. LAUNCHER ";
// String action = "android. intent. action. MAIN ";
// Intent newintent = new Intent (context, AtestActivity. class );
// Newintent. setAction (action );
// Newintent. addCategory (category );
// Newintent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
//----------------------------------------------------------------------
// The method I have tried. I cannot try it on my cell phone. It works on the simulator.
Intent newintent = new Intent (context, AtestActivity. class );
Newintent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
Context. startActivity (newintent );
Log. I ("Cycler", "oncycler over ");
}
}
Configuration File
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. test"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "10"/>
<Uses-Permission Android: Name = "android. Permission. disable_keyguard"/>
<! -- Some people say that they want to add the following line here. I didn't add it. You can also -->
<! -- <Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/> -->
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". AtestActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Cycler android: name = ". StartListenerActivity">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Cycler>
</Application>
</Manifest>
The effect is as follows: