gpt4 book ai didi

java - 从服务获取 Activity 的小部件

转载 作者:行者123 更新时间:2023-12-01 04:50:37 25 4
gpt4 key购买 nike

主要 Activity 有一个 AlarmManager,它每 X 分钟调用一次服务。我需要一种方法,使服务类中的方法可以更新主 Activity 中的 TextView,但我不知道如何获取服务中的 TextView 对象。有什么办法吗?

这是部分代码:

Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pintent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 60000, pintent);

最佳答案

在您的警报服务中,您有一个 onReceive 方法

   public void onReceive(Context context, Intent arg1) {

String data = "haha";
if (data.isEmpty() == false && data.contentEquals("") == false) {

nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "sing";
CharSequence message = data;
//get the activity
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.icon,
data, System.currentTimeMillis());
notif.defaults |= Notification.DEFAULT_SOUND;
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);

}

方法调用:

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ReminderReceiverActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
100000, pendingIntent);
}

关于java - 从服务获取 Activity 的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042947/

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