BroadcastRecevier 不想更改值
BroadcastRecevier don't want to change value
当我通过广播将数据从一个 activity 传递到第二个 activity 并且当我第一次输入文本时,我遇到了这个问题 - 一切顺利,但当我回来时并输入另一个文本,然后最后一个文本保持不变。我在这里和那里祝酒词,Broadcastreceiver 似乎无法更改此字符串值。
主要活动:
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);
}
广播接收器:
@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();
}
第二个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();
}
待定 Intent 缓存在 Android 中。当你想重复使用相同 id 的待定意图时,你必须更新它:
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
当我通过广播将数据从一个 activity 传递到第二个 activity 并且当我第一次输入文本时,我遇到了这个问题 - 一切顺利,但当我回来时并输入另一个文本,然后最后一个文本保持不变。我在这里和那里祝酒词,Broadcastreceiver 似乎无法更改此字符串值。
主要活动:
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);
}
广播接收器:
@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();
}
第二个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();
}
待定 Intent 缓存在 Android 中。当你想重复使用相同 id 的待定意图时,你必须更新它:
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);