英文:
How to call MainActivity method from AlarmManager BroadcastReceiver?
问题
我有一个在每天特定时间触发的闹钟。我已经测试过触发功能,它是有效的。
现在,当闹钟触发时,我想从MainActivity
中调用updateTextView()
,它会更改activity_main.xml
中的一个TextView
。我已经尝试了这里的许多解决方案:要么我没有理解它们,要么它们不起作用。
MainActivity:
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);
}
}
BroadcastReceiver:
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!!!!!!!!!!!!!!!!!!!!!!");
}
}
英文:
I have an alarm that fires at a specific time of day everyday. I have tested the firing and it works.
Now, when the alarm fires I want to call updateTextView()
from MainActivity
, which changes a TextView
in activity_main.xml
.
I have tried many of the solutions here: Either I am not understanding them or they are not working.
MainActivity:
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);
}
}
BroadcastReceiver:
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!!!!!!!!!!!!!!!!!!!!!!");
}
}
答案1
得分: 3
在你的接收器中:
@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();
}
英文:
In your Receiver
@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);
}
In you MainActivity oncreate()
Intent intent = getIntent();
int TYPE = intent.getIntExtra("type",4);
if(TYPE == 4)
{
updateTextView();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论