待执行的Intent传递字符串结果未获得

时间:2015-03-23 19:39:50

标签: android android-intent android-activity

我正在尝试将String从一个活动传递到另一个活动。 我读了它是如何完成的,但是当我实现时,我无法获得所需的输出。

我的第一堂课:

public class Settings extends Activity implements View.OnClickListener {


private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);
    Intent alarmIntent = new Intent(Settings.this, AlarmReceiver.class);
    alarmIntent.putExtra("key", "Tuesday");
    pendingIntent = PendingIntent.getBroadcast(Settings.this, 0, alarmIntent, 0);
    alarmRepeat();
}
  public void alarmRepeat(){
    Calendar cal = Calendar.getInstance();
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000, pendingIntent);
}

我的第二个活动,我已通过“key”字符串:

public class AlarmReceiver extends BroadcastReceiver {

AudioManager audioControl;
Bundle extras;
String days;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Date d = new Date();
    String day = sdf.format(d);
    Calendar c = Calendar.getInstance();
    audioControl = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);

    days = intent.getStringExtra("key");
    if(day.equals("days")){audioControl.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);}
}

根据程序,手机的音频设置应改为振动,但没有任何反应。

1 个答案:

答案 0 :(得分:2)

您的代码中有拼写错误。你的最后一行应该是if(day.equals()){... 调试器还将帮助您分析相等测试未触发的原因。 更正了以下代码:

@Override
public void onReceive(Context context, Intent intent) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Date d = new Date();
    String day = sdf.format(d);
    Calendar c = Calendar.getInstance();
    audioControl = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    days = intent.getStringExtra("key");
    if(day.equals(days) {audioControl.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);}
}