一、基础
App定时提醒(AlarmManager实现,适配不同版本)
二、使用
//build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.tomorrow.second"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tomorrow.second">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".TestService">
<intent-filter>
<action android:name="com.tomorrow.intent.service" />
</intent-filter>
</service>
</application>
</manifest>
//MainActivity
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "zwm, onCreate");
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("com.tomorrow.intent.service");
PendingIntent sender = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, sender);
} else {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, sender);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "zwm, onDestroy");
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("com.tomorrow.intent.service");
PendingIntent sender = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(sender);
}
}
//TestService
public class TestService extends Service {
private static final String TAG = "TestService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "zwm, onStartCommand");
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent newIntent = new Intent("com.tomorrow.intent.service");
PendingIntent sender = PendingIntent.getService(this, 1, newIntent, PendingIntent.FLAG_CANCEL_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, sender);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, sender);
}
return super.onStartCommand(intent, flags, startId);
}
}
//输出log
2019-11-18 14:33:26.318 10012-10012/com.tomorrow.second D/MainActivity: zwm, onCreate
2019-11-18 14:33:26.791 10012-10012/com.tomorrow.second D/TestService: zwm, onStartCommand
2019-11-18 14:33:36.859 10012-10012/com.tomorrow.second D/TestService: zwm, onStartCommand
2019-11-18 14:33:46.872 10012-10012/com.tomorrow.second D/TestService: zwm, onStartCommand
2019-11-18 14:33:49.920 10012-10012/com.tomorrow.second D/MainActivity: zwm, onDestroy