gpt4 book ai didi

java - AlarmManager 以及如何设置它

转载 作者:行者123 更新时间:2023-12-02 08:04:05 26 4
gpt4 key购买 nike

我很难专心于对 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");
}
}

这是我的 list 文件:

        <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)

最佳答案

您似乎正在尝试将广播接收器当作一项服务来使用。一种选择是将 BR 转变为服务并更改待定 Intent 以启动该服务。

关于java - AlarmManager 以及如何设置它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8447896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com