I have 1 activity that I would like to start at different times with different variables from a Broadcast Receiver.

  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equalsIgnoreCase("tv.abcd.v4.ORIGINAL_VIDEO_SCENE")){
                channelName = intent.getExtras().getString("com.abcd.Channel");
            JSONObject json = new JSONObject(intent.getExtras().getString("com.abcd.Data"));
            String incomingScene = json.getString("url");
            scene.putExtra("channel", channelName);
            scene.putExtra("url", incomingScene);
            scene.addFlags(Intent.FLAG_FROM_BACKGROUND);
            scene.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
            scene.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            context.startActivity(scene);

  }

I have the code to start the activity via Intent and in the activity receiver the extras to make data appear.

            Intent intent = getIntent();
sceneUrl = intent.getStringExtra("url");
Log.d("Image.incomingscene.url",sceneUrl);
channelName = intent.getStringExtra("channel");
Log.d("Image.incomingSceneAvatar",networkAvatar);
image = (ImageView)findViewById(R.id.imageView1);
image.setScaleType(ScaleType.FIT_CENTER);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Picasso.with(this).load(sceneUrl).skipMemoryCache().fit().into(image, new EmptyCallback() {

}); 

Now after that I want to start the same activity again from the Broadcast Reciever with different data. So i want the previous activity to get out the way and allow this new instance to start up.

How to accomplish this feat?

有帮助吗?

解决方案

register another broadcast receiver from the activity. Then, when you want to kill it, send a broadcast message from the broadcast receiver that you mentioned .

In your broadcastReceiver do something like the following :

  public static final String CLOSE_Activity= "com.mypackage.closeactivity";

and in yopr OnReceive method do like the following :

@Override   
 public void onReceive(Context context, Intent intent) {  
       System.out.println("HIT OUTGOING");      
   Intent i = new Intent();      
   i.setAction(CLOSE_Activity);       
  context.sendBroadcast(i);    
 } 

then in your activity craete a receviver and register it in the onResume method and unregeister it in the onPause method , like the following :

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(RECEIVER_Class.CLOSE_Activity)) {
            finish();
        }
    }
}

activity onResume method :

@Override
public void onResume() {
    registerReceiver(broadcastReceiver, new IntentFilter(RECEIVER_Class.CLOSE_Activity));
}

activity onPause method :

 @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
}

Please give me some feedback

Hope that helps .

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top