2 baifaxiaoyao baifaxiaoyao 于 2012.11.28 10:45 提问

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
Sueyexin   2012.11.28 13: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();
    }

}
baifaxiaoyao
baifaxiaoyao 非常感谢,应该可以了。试过后给你采纳。
接近 5 年之前 回复
Sueyexin
Sueyexin 我更新了下我上边的答案。在注册intent的时候我写错了。你再试下。
接近 5 年之前 回复
baifaxiaoyao
baifaxiaoyao 不行,我好像还是不能让它起作用。当我在scheduleupdatemethod设置我的intent位intent = new Intent(MainActivity.this,MainActivity.class);时<receiver>引起了错误,所以我删除了。广播接收器似乎不能被调用?
接近 5 年之前 回复
Sueyexin
Sueyexin 我相信它应该是直接指向MainActivity,我不认为在Manifest的<receiver>元素是必要的,因为在这种情况下,它是和activity有联系的
接近 5 年之前 回复
baifaxiaoyao
baifaxiaoyao 谢谢Sueyexin。但是我还有几个问题。我是的intent现在应该指向Mainactivity还是接收器?还是应该manifest被改变成MainActivity.class或者是receiver.class?
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片