我想在凌晨12点自动调用我的应用程序,这种过程,如何在android中实现,可以吗?
问问题
769 次
2 回答
1
看看AlarmManager。
这是一个例子:
Intent myIntent = new Intent(getApplicationContext(), YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
于 2010-12-20T10:08:46.573 回答
1
ReminderBootUp.java 文件包含在特定时间安排您的活动的代码
package com.app.reminder;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class ReminderBootUp extends BroadcastReceiver {
private SQLiteDatabase myDataBase = null;
private Cursor rs = null;
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
try {
long alarmTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(
"2010-12-21 12:00").getTime();
int id = 1;
Date date = new Date();
if (date.getTime() <= alarmTime) {
Intent notifyIntent = new Intent(context, youractivity.class);
PendingIntent intent = PendingIntent.getBroadcast(context, id,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime, intent);
}
} catch (Exception e) {
Log.e("Exception", e.getMessage(), e);
}
}
}
你应该在 Manifest.xml 文件中添加这一行
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
<receiver android:name="ReminderBootUp" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
此代码将在启动设备后工作...
如果您想以正常的状态活动启动您的应用程序,请按照我们的正常代码编写
于 2010-12-21T08:32:22.160 回答