问题描述
- 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.thisAlarmReceiver.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()+1000060000pendingIntent); Streamer str = new Streamer(); boolean alarmUp = (PendingIntent.getBroadcast(this 0 new Intent(MainActivity.thisAlarmReceiver.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);}}
解决方案
你可以在你的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(); }}
时间: 2024-10-02 22:01:36