baifaxiaoyao 2012-11-28 02:45 采纳率: 100%
浏览 4172
已采纳

android:将两个类合并到一个activity中有什么合适的方法?

我有两个类,一个是MainActivity扩展了activity,另外一个是AlarmReceiver扩展了BroadcastReceiver。我怎么能够消除AlarmReceiver,而是在MainActivity实现这个功能?我知道不可能同时扩展两个类,所以我想知道最好的方法应该是什么?

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scheduleNextUpdate();
         Streamer str = new Streamer();
            Calendar calendar = str.getIcs();


        int x = 1;
        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

        for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
            Component component = (Component) i.next();
            TextView tv_1 = new TextView(this);
            layout.addView(tv_1);
            tv_1.setText("Component " + x);
            x++;;

            for (Iterator j = component.getProperties().iterator(); j.hasNext();) {
                Property property = (Property) j.next();
                TextView tv_2 = new TextView(this);
                layout.addView(tv_2);
                tv_2.setText(property.getName() + ": " + property.getValue());
            }
        }

    }

    public void scheduleNextUpdate()
      {

        Intent intent = new Intent(MainActivity.this,AlarmReceiver.class);
        PendingIntent pendingIntent =
            PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        long currentTimeMillis = System.currentTimeMillis();
        long nextUpdateTimeMillis = (currentTimeMillis *2) + DateUtils.MINUTE_IN_MILLIS;
        Time nextUpdateTime = new Time();
        nextUpdateTime.set(nextUpdateTimeMillis);

        if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 22)
        {
          nextUpdateTime.hour = 8;
          nextUpdateTime.minute = 0;
          nextUpdateTime.second = 0;
          nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
        }
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
       // alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000,60000,pendingIntent);

        Streamer str = new Streamer();
        boolean alarmUp = (PendingIntent.getBroadcast(this, 0, 
                new Intent(MainActivity.this,AlarmReceiver.class), 
                PendingIntent.FLAG_NO_CREATE) != null);

        if (alarmUp)
        {
            Log.d("myTag", "Alarm is already active");
        }
        System.out.println(currentTimeMillis);
      }

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
     public void onReceive(Context context, Intent intent) {  
        Log.d("myTag", "Alarm has been received");
    intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
     Streamer str = new Streamer();
        Calendar calendar = str.getIcs();
        System.out.println(calendar);
}

}
  • 写回答

1条回答 默认 最新

  • Sueyexin 2012-11-28 05:07
    关注

    你可以在你的activity注册BroadcastReceiver然后在onPause注销

    public class MainActivity extends Activity {
    
        private BroadcastReceiver receiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(getApplicationContext(), "received", Toast.LENGTH_SHORT).show();
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent = new Intent("TESTING");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, 10000, pendingIntent);
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(receiver, new IntentFilter("TESTING"));
        }
    
        @Override
        protected void onPause() {
            unregisterReceiver(receiver);
            super.onPause();
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动