如何从 AlarmManager BroadcastReceiver 调用 MainActivity 方法?
How to call MainActivity method from AlarmManager BroadcastReceiver?
我有一个每天在特定时间触发的闹钟。我已经测试了射击并且它有效。
现在,当警报响起时,我想从 MainActivity
调用 updateTextView()
,这会更改 activity_main.xml
中的 TextView
。
我在这里尝试了很多解决方案:要么我不理解它们,要么它们不起作用。
主要活动:
public class MainActivity extends AppCompatActivity {
public TextView dailyTextView;
//TESTING
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dailyTextView = findViewById(R.id.main_text);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE,25);
startAlarm(calendar);
}
public void updateTextView() {
...
}
private void startAlarm(Calendar calendar) {
Toast.makeText(this,"ALARM TEST",Toast.LENGTH_LONG).show();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DailyUpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1, intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
广播接收器:
public class DailyUpdateReceiver extends BroadcastReceiver {
private static final String TAG = "MyActivity";
//TODO: WORK ON THIS
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "ON RECEIVE STARTED!!!!!!!!!!!!!!!!!!!!!!");
}
}
在您的接收器中
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MainActivity.class);
intent1.putExtra("type", 4);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
在你的MainActivity中oncreate()
Intent intent = getIntent();
int TYPE = intent.getIntExtra("type",4);
if(TYPE == 4)
{
updateTextView();
}
我有一个每天在特定时间触发的闹钟。我已经测试了射击并且它有效。
现在,当警报响起时,我想从 MainActivity
调用 updateTextView()
,这会更改 activity_main.xml
中的 TextView
。
我在这里尝试了很多解决方案:要么我不理解它们,要么它们不起作用。
主要活动:
public class MainActivity extends AppCompatActivity {
public TextView dailyTextView;
//TESTING
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dailyTextView = findViewById(R.id.main_text);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE,25);
startAlarm(calendar);
}
public void updateTextView() {
...
}
private void startAlarm(Calendar calendar) {
Toast.makeText(this,"ALARM TEST",Toast.LENGTH_LONG).show();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DailyUpdateReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1, intent,0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
广播接收器:
public class DailyUpdateReceiver extends BroadcastReceiver {
private static final String TAG = "MyActivity";
//TODO: WORK ON THIS
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "ON RECEIVE STARTED!!!!!!!!!!!!!!!!!!!!!!");
}
}
在您的接收器中
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MainActivity.class);
intent1.putExtra("type", 4);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
在你的MainActivity中oncreate()
Intent intent = getIntent();
int TYPE = intent.getIntExtra("type",4);
if(TYPE == 4)
{
updateTextView();
}