jean d'arme
jean d'arme

Reputation: 4343

BroadcastRecevier don't want to change value

I have this problem when I pass data from one activity through broadcast to second activity and when I enter the text for the first time - it goes all great, but when I come back and enter another text then last text remains unchanged. I put toasts here and there and it appear that Broadcastreceiver can't change this String value.

MainActivity:

public void setCall(int timeToCall){
    if (alarmManager!= null) {
        alarmManager.cancel(pendingIntent);
    }
    String name = e1.getEditableText().toString();
    //Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(MainActivity.this, ThisBroadcastReceiver.class);
    intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    intent.putExtra("name",name);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + timeToCall, pendingIntent);
}

BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    String name = intent.getExtras().getString("name");

    Intent i = new Intent(context, CallScreen.class);
    i.setClassName("(package)", "(classname)");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("name2", name);
    context.startActivity(i);
    Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}

Second activity:

public class CallScreen extends Activity {

TextView incomingCall, caller;
MediaPlayer mp;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_screen);


    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        name = extras.getString("name2");
    }

    incomingCall = (TextView) findViewById(R.id.textIncomingView);
    caller = (TextView) findViewById(R.id.callerId);
    caller.setText(name);

    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mp = MediaPlayer.create(getApplicationContext(), notification);
    mp.start();
}

@Override
protected void onPause() {
    super.onPause();
    if (mp != null) {
        mp.release();
        mp = null;
    }
    finish();
}

@Override
protected void onStop() {
    super.onStop();
    if (mp != null) {
        mp.release();
        mp = null;
    }
    finish();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mp != null) {
        mp.release();
        mp = null;
    }
    finish();
}

Upvotes: 1

Views: 71

Answers (1)

user3390963
user3390963

Reputation: 2329

Pending Intents are cached in Android. When you want to reuse pending intent with the same id, you must update it with:

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Upvotes: 2

Related Questions