我很难围绕编程我的AlarmManager。
下面的代码几乎只是将当前日期分配给文本字段并获取一些XML并使用setListAdapter
显示它。我想让这段代码在启动时运行,然后每30分钟运行一次。
Date anotherCurDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEEE', 'MMMM dd', ' yyyy");
String formattedDateString = formatter.format(anotherCurDate);
TextView currentRoomDate = (TextView) this.findViewById(R.id.CurrentDate);
currentRoomDate.setText(formattedDateString);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML(items);
Document doc = XMLfunctions.XMLfromString(xml);
/*int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(MeetingManager.this, "NOT GETTING XML", Toast.LENGTH_LONG).show();
finish();
} */
Element docElem = doc.getDocumentElement();
NodeList nodes = (NodeList)docElem.getElementsByTagName("meeting_item");
int node_number = nodes.getLength();
String node_final = String.valueOf(node_number);
Log.d(TAG, node_final);
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
String date_value = XMLfunctions.getValue(e, "date");
// Log.d(TAG, date_value);
//Log.d(TAG, formattedDateString);
//if (date_value == formattedDateString){
map.put("time", XMLfunctions.getValue(e, "time"));
map.put("endtime", XMLfunctions.getValue(e, "endtime"));
map.put("name", XMLfunctions.getValue(e, "meeting_name"));
map.put("hostname", XMLfunctions.getValue(e, "host_name"));
mylist.add(map);
//}
}
ListAdapter adapter = new SimpleAdapter(MeetingManager.this, mylist , R.layout.listlayout,
new String[] {"time","endtime", "name", "hostname" },
new int[] { R.id.time, R.id.endtime, R.id.meeting_name, R.id.host });
setListAdapter(adapter);
Intent mRefreshIntent = new Intent().setComponent(new ComponentName(getBaseContext(), UpdateData.class)).setAction("com.MeetingManager.UpdateData");
PendingIntent mPendingRefreshIntent = PendingIntent.getBroadcast(getBaseContext(), 0, mRefreshIntent, PendingIntent.FLAG_CANCEL_CURRENT); //choose your desired flag
AlarmManager mAlarmManager = null;
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), AlarmManager.INTERVAL_HALF_HOUR, mPendingRefreshIntent);
这是我的UpdateData类:
package com.MeetingManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class UpdateData extends BroadcastReceiver {
private static final String TAG = "MyApp";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "HELLO UPDATE");
}
}
这是我的清单文件:
<receiver android:name="UpdateData">
<intent-filter>
<action android:name="com.MeetingManager.UpdateData" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
任何人都可以就我做错的事情向我提出建议。
以下是我的调试输出:
<terminated>MeetingManager [Android Application]
<disconnected>DalvikVM[localhost:8615]
MeetingManager [Android Application]
DalvikVM[localhost:8615]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125
ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running)
Thread [<5> Binder Thread #1] (Running)
答案 0 :(得分:1)
首先,您需要创建PendingIntent
:
mRefreshIntent = new Intent()
.setComponent(new ComponentName(mContext, YourBroadcastReceiver.class))
.setAction("my.package.name.YOUR_ACTION");
mPendingRefreshIntent = PendingIntent.getBroadcast(
mContext,
0,
mRefreshIntent,
PendingIntent.FLAG_CANCEL_CURRENT); //choose your desired flag
然后,您可以使用以下代码
设置闹钟mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, yourInterval, mPendingRefreshIntent);
同样,您可以选择除RTC_WAKEUP之外的其他一些标志。
要执行代码,您需要自己实现BroadCastReceiver
:
public class YourBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//execute your code here
}
}
您需要在清单中注册该接收器,以获取警报生成的意图,以及在系统启动时广播的意图:
<manifest>
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<application>
...
<receiver
android:name="YourBroadcastReceiver">
<intent-filter>
<action android:name="my.package.name.REFRESH_WALLPAPER" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
...
</application>
</manifest>
许可很重要!
希望这能回答你的问题。
答案 1 :(得分:1)
看起来您正在尝试使用广播接收器,就好像它是一项服务一样。一种选择是将BR转变为服务并更改待定意图以启动该服务。