App.class,
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
PendingIntent myPendingIntent = PendingIntent.getService(this, 0, MyService.newIntent(this), 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
MyService.class,
public class MyService extends IntentService {
private static final String TAG = MyService.class.getSimpleName();
private static final String EXTRA_CHECK = "checkStrikeAndNews";
public MyService() {
super("MyService");
}
public static Intent newIntent(Context context) {
return new Intent(context, MyService.class)
.putExtra(EXTRA_CHECK, true);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.getBooleanExtra(EXTRA_CHECK, false)) {
Log.i(TAG, "I just received an intent");
}
}
}
Manifest.xml
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.MyService" />
</application>
</manifest>
01/02/2015
BroadcastReceiver
App.class
public class App extends Application {
private static final int INTERVAL = 1000 * 60;
private static final int DELAY = 5000;
@Override
public void onCreate() {
super.onCreate();
setupAlarm();
}
private void setupAlarm() {
Intent myIntent = new Intent(this, MyReceiver.class);
PendingIntent myPendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, DELAY, INTERVAL, myPendingIntent);
}
}
: SDK , 5.1, 1 .
60000 Android 5.1; , . API 22, AlarmManager , 5 , 60 . 5 , .
MyReceiver.class
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
}
}
Manifest.xml
<manifest
package="com.darzul.test"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:theme="@style/AppTheme">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:exported="false"
android:name=".receiver.MyReceiver" />
</application>
</manifest>