嘿那裏我試圖每隔15分鐘使用alaram管理器運行android後臺服務,但我無法做到這一點我不會發生什麼錯誤,我不知道什麼是錯的在我的代碼它不工作android報警管理器每15分鐘運行一次後臺服務
嘗試{
//Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, RingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
3000,pendingIntent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
RingAlaram.class
public class RingAlarm extends Service {
public void onCreate() {
Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
和最後我的清單
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarmmanagerexample.AlarmManagerExample"
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=".RingAlarm"
android:exported="false" />
</application>
設置標誌PendingIntent.FLAG_UPDATE_CURRENT –
@ AC-開放源代碼還是它不工作 –
嘗試以下 –