call activity method from broadcast receiver

后端 未结 5 1876
小蘑菇
小蘑菇 2020-12-03 11:33

In the main activity, a layout is loaded that has some input fields and a submit button. When the submit button is clicked, the onClick handler method sends an sms back to t

5条回答
  •  Happy的楠姐
    2020-12-03 11:56

    You can't directly call a function in your Activity unless it's a public static method but don't do so. I recommend this:

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = new Bundle();
        bundle.putExtraString("ACTION", "stopBroadcast");
    
        // ### put what ever you need into the bundle here ###
    
        Intent intent = new Intent();
        intent.setClassName(context, "activity.class");
        intent.putExtras(bundle);
    
        context.startActivity(intent);
    }
    

    And then from your Activity's onCreate() get Bundle and take your actions as needed.

提交回复
热议问题