简体   繁体   中英

Start pick image activity from Broadcast Receiver

Hi I have an app that has a custom notification with buttons. Now I have been able to detect that the broadcast receiver receives the button presses on the notification. But my idea is that when the user presses one of the buttons it will initiate the CropImage.startPickImageActivity, I am using the Android Image Cropper Library.

But everytime I try to start the pick image activity on the button press from the notification. My app crashes, can anyone tell why is this happening or what am I doing wrong?

This is my code for my broadcast receiver:

public class NotificationReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if (action.equals("btn2")){
        CropImage.startPickImageActivity((Activity) context);
    }
  }

 }

Thanks in advance for any help guys! :D

That's because you can not just downcast context in Activity it it's not an Activity . Change this line.

CropImage.startPickImageActivity((Activity) context);

Now the solution can be for this problem.

  • If you want to handle both Background and forground scenario . Then you should open an Activity from broadcast receiver . And in it's onCreate() you can pass the data and hopefully open your Cropper too .

  • Second is you can directly open Cropper activity with intent(if it's a activity only) . If you do not need any result back from it you can directly open it . I think that's it . Thx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM