接收方没有获取数据

时间:2017-05-27 23:16:56

标签: android android-intent broadcastreceiver

所以我的接收器工作正常...正在发送和接收待处理的意图并且所有工作正常,直到此...我有一个包含警报声的微调器,当用户点击其中的项目时,我想获取位置并将其发送到接收器..问题是接收器始终获取值为0 ..可以有人帮助

这是我的闹钟活动中的微调器

int choose_alarm_sound;

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



                String selected = parent.getItemAtPosition(position).toString();
                choose_alarm_sound = position;
                Intent broad_Intent = new Intent();
                Log.e("TEST 1", choose_alarm_sound + "");



                switch (selected) {
                    case "Buzzer": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.buzzer);
                        mp.start();

                        break;
                    }
                    case "Doorbell": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.doorbell);
                        mp.start();

                        break;
                    }
                    case "FirePager": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.firepager);
                        mp.start();

                        break;
                    }
                    case "Ring": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.ring);
                        mp.start();

                        break;
                    }
                    case "SchoolBell": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.schoolbell);
                        mp.start();

                        break;
                    }
                    case "Siren": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.siren);
                        mp.start();

                        break;
                    }
                    case "Siren2": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.siren2);
                        mp.start();

                        break;
                    }
                    case "SmokeAlarm": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.smokealarm);
                        mp.start();

                        break;
                    }
                    case "Warning": {
                        mp = MediaPlayer.create(alarmClockActivity.this, R.raw.warning);
                        mp.start();

                        break;
                    }

                }
                broad_Intent.putExtra("alarm_choice", choose_alarm_sound);
                Log.e("TEST 2", choose_alarm_sound + "");
                sendBroadcast(broad_Intent);

这是我的接收器

 public class AlarmReceiver extends BroadcastReceiver {

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

    Integer getAlarm=arg1.getExtras().getInt("alarm_choice");

    Log.e("TEST 3", String.valueOf(getAlarm));

}

所以我的日志测试1和测试2总是具有相同的值,这是正确的值,但接收器中的测试3每次都是0 ..所以我的接收器没有接收到这个意图..为什么......任何帮助都会非常感谢

清单:

 <?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/mini"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".TimeDateRem"/>
    <activity android:name=".BirthdayRem"/>
    <activity android:name=".Details"/>
    <activity android:name=".DailyReminder"/>
    <activity android:name=".HolidayReminderLebanon"/>
    <activity android:name=".HolidayReminderSaudiArabia"/>
    <activity android:name=".HolidayReminderKuwait"/>
    <activity android:name=".HolidayReminderUAE"/>
    <activity android:name=".AlarmChooser"/>
    <activity android:name=".Timer"/>
    <receiver android:name=".AlarmReceiver">
</receiver>
    <service android:name=".MediaPlayingService"/>
</application>

2 个答案:

答案 0 :(得分:0)

因为您将choose_alarm_sound设为id而非position

答案 1 :(得分:0)

您需要发送带有ACTION的广播example <- list("a", c("b", "c"), c("d", "e", "f"), c("")) example <- setNames(example, seq_along(example)) ex2 <- stack(example) ex2[ex2$values=='','values'] <- NA library(reshape2) dcast(ex2, ind ~ values, fun.aggregate = length) ,并且您需要告知Android在看到该动作时致电 ind a b c d e f NA 1 1 1 0 0 0 0 0 0 2 2 0 1 1 0 0 0 0 3 3 0 0 0 1 1 1 0 4 4 0 0 0 0 0 0 1 。尝试这样的事情:

在清单中:

Intent

如果有人发送广告BroadcastReceiver,其中ACTION为“MyAlarmReceiver”,则告诉Android调用<receiver android:name=".AlarmReceiver"> <intent-filter> <action android:name="MyAlarmReceiver"/> </intent-filter> </receiver>

现在,当您在AlarmReceiver.onReceive()中创建广播Intent时,请执行以下操作:

Intent

这会将广播onItemSelected()中的ACTION设置为“MyAlarmReceiver”。