Hammad Nasir
Hammad Nasir

Reputation: 2959

Unable to get intent sent from a BroadcastReceiver

I have a Service which looks for changes in database and then notify users based on certain factors. One such notification has an action button by clicking on which a BroadcastReceiver is triggered and Activity.class gets opened.

Here's how:

public class MyBroadcastReceiver extends BroadcastReceiver {

        public MyBroadcastReceiver(){
            super();
        }

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

             if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id_for_rating))) {
                Intent resultIntent = new Intent(getBaseContext(), Activity.class);
                resultIntent.putExtra("abc", "abc");
                resultIntent.setType("text/plain");
                resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(resultIntent);
            } else {
                Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
            }
        }
    }

Here's how I'm trying to receive intent in a ViewHolder class called from this Activity.class:

public class RModelClass extends AbstractItem<RModelClass, RModelClass.ViewHolder> {

    public RModelClass() {}

    @Override
    public int getType() {
        return R.id.recycler_view_r;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.r_played;
    }

    @Override
    public void bindView(final RModelClass.ViewHolder holder, List payloads) {
        super.bindView(holder, payloads);

        String abc;
        Intent intent = new Intent();
        if (intent.getExtras() != null) {
            openForRating = intent.getExtras().getString("abc");

            if (abc != null) {
                Log.d("abc", "YO!!");

            } else {
                Log.d("abc", "NO!!");
            }

        } else {
            Log.d("null", "INTENT!!");
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);

        }

    }

}

The problem is that I'm getting D/null: INTENT!! logged out and unable to receive the intent.

Please help me why is this happening and how to get the intent?

Upvotes: 1

Views: 517

Answers (2)

Chetan Joshi
Chetan Joshi

Reputation: 5711

you should use getApplicationContext() in BroadcastReceiver to create Intent .

Intent resultIntent = new Intent(getApplicationContext(), Activity.class);
resultIntent.putExtra("abc", "abc");
resultIntent.setType("text/plain");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(resultIntent);

Also you are doing mistake in this line

Intent intent = new Intent();

this is new intent object and you are trying to get values from this Intent object.

you must get Intent from your Android Component like getIntent()

if you are in Fragment Class or in Adapter then use getActvity().getIntent() and ((Activity)context).getIntent() respectively for fragment and Adapter.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007533

why is this happening

You create a brand-new Intent object:

Intent intent = new Intent();

Then you try looking for extras on that brand-new Intent object:

if (intent.getExtras() != null) {

That Intent object is brand-new. It will not have any extras.

how to get the intent?

To get the Intent that was used to create the activity, call getIntent().

Upvotes: 0

Related Questions