【问题标题】:Change a view content from an intent, if in inner class it's not thrown从意图更改视图内容,如果在内部类中它没有被抛出
【发布时间】:2013-11-20 09:16:07
【问题描述】:

我想在几分钟后发出警报意图。从这个意图接收一些数据,应用程序的视图应该显示它。

此代码在活动中

public class MainActivity extends Activity{

    DatePicker pickerDate;
    TimePicker pickerTime;
    Button buttonSetAlarm;
    TextView info;

...
private void setAlarm(){
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);   
}

然后广播接收器,在 Activity 类中或在一个单独的 java 文件中(见帖子末尾),看起来像这样:

public class AlarmReceiver extends BroadcastReceiver {

    private static final int MY_NOTIFICATION_ID=1;
    NotificationManager notificationManager;
    Notification myNotification;

    @Override
    public void onReceive(Context context, Intent intent) {
          Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
          String information = getInformation();
          info.setText("this is your info"+information); <--------------------
    }
}

如何做到这一点?

我尝试通过膨胀从上下文中获取视图,但没有成功

关注android - How to get view from context?

我也尝试过从意图内的捆绑包中获取视图,但它们似乎只能传递字符串和可序列化对象。

如果我 在活动中的广播接收器内部,则不会引发警报(没有吐司消息出现,textview 中没有变化)描述在:Calling SetContentView() from broadcast receiver 尽管似乎这个解决方案没有' t 工作如:BroadcastReceiver as inner class

提前谢谢你!

L.

【问题讨论】:

    标签: android android-intent android-view


    【解决方案1】:

    您无法从 Activity 外部访问 UI 组件。要执行您想要的操作,您需要让 onReceive() 方法调用 Activity 本身的方法,然后它会完成工作。

    一般来说这是错误的方法。您可能不需要使用AlarmManagerBroadcastReceiver 来完成此操作。您应该在您的活动中创建一个Handler,然后将Runnable 或一条消息发布到您的Handler。您可以发布一些内容,以便在未来某个时间执行 (Runnable) 或发送(消息)。

    【讨论】:

    • 谢谢大卫,你的答案对我来说很清楚。目前我正在构建一个将来需要 AlarmManager 的原型,从警报更改视图的要求仅用于调试目的。这就是为什么我发现我的答案更适合我的情况,但我检查你的答案是否正确,你能看看我评论过的解决方案吗?只是想知道您的意见,特别是从广播接收器重新启动应用程序是否是一种可行/优雅的解决方案。感谢您的帮助。
    • 这里有一个解释如何做 David Wasser 提案的教程:robotoworks.com/2012/11/exploring-android-handlers
    【解决方案2】:

    解决方案是在类的构造函数上实例化一个广播接收器并以编程方式注册它。

    问题是它在暂停时会停止工作,但无论如何我发现更改已销毁应用程序的文本视图是荒谬的。

    如果我想在应用程序被破坏的情况下显示警报,我会在其自己的 .java 文件中使用广播,在活动之外,并在清单中注册,如果需要,将重新启动应用程序,然后在最近打开的应用会刷新它的内容。

          Commig from the constructor ...
    
                br = new BroadcastReceiver() {
                    private static final int MY_NOTIFICATION_ID=1;
                    NotificationManager notificationManager;
                    Notification myNotification;
    
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
    
                        info.setText("DONE!");
                    }
            };
    
        }
    
        @Override
        public void onResume(){
            super.onResume();
            registerReceiver(br, new IntentFilter("onalarm"));   
        }
    
        @Override
        public void onPause(){
            unregisterReceiver(br);
            super.onPause();
        }
    
        private void setAlarm(){
            info.setText("cuasimodo");
            //Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            Intent intent = new Intent("onalarm");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000, pendingIntent);    
        }
    

    【讨论】:

    • 是的,您可以这样做。如果您想在应用程序未运行时执行此操作,则需要从 onReceive() 启动应用程序。您可以在Intent 中传递一些额外信息,告诉应用程序正在从警报启动,以便应用程序可以做一些智能的事情。
    猜你喜欢
    • 2021-08-02
    • 2022-11-30
    • 2023-03-20
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多