如何从AlarmManager BroadcastReceiver中调用MainActivity方法?

huangapple go评论73阅读模式
英文:

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();
}    

huangapple
  • 本文由 发表于 2020年9月7日 02:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定