4

我对附加到警报的文本字符串有疑问。似乎触发警报时,附加到警报的字符串为空。显然我犯了一个错误-但不知道在哪里。

我设置闹钟的代码是;

static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str)
{
    Intent intent = new Intent(cont, to_call_when_alarm_goes_off.class);
    intent.putExtra("string_passed_in_bundle", str); 

    Log.i("xx","set_alarm ["+str+"]"); // The string I see in the Log is correct
    PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, intent, 0);

    alarm_manager.cancel(pIntent);

    alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent);
}

接收告警的代码如下:

public class to_call_when_alarm_goes_off extends BroadcastReceiver
{
    Bundle bundle_from_whoever_called_this_activity;

    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
        String str;

        bundle_from_whoever_called_this_activity = arg1.getExtras();

        str = bundle_from_whoever_called_this_activity.getString("string_passed_in_bundle");

        Log.i("xx","to_call_when_alarm_goes_off: TIME TO WAKE UP!!! ["+str+"]");

        try 
        {
            Intent i = new Intent(arg0, dingaling.class);
            i.putExtra("string_passed_in_bundle", str);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            arg0.startActivity(i);
        } 
        catch (Exception e) 
        {
        }
    }
}

当我设置警报然后等待它被触发时,日志文件将显示如下内容

set_alarm [Go to meeting]
to_call_when_alarm_goes_off: TIME TO WAKE UP!!! [null]

编辑:会不会有一些“超级”。我忘记调用的函数?

编辑:我经常对传递给各种函数的上下文类型感到困惑,例如 getBaseContext()、getApplicationContext()、"this" 等...如果我在某处涉及错误类型的上下文,会导致这个问题吗?

4

5 回答 5

2

我只是将您的代码复制并粘贴到我的应用程序中,它运行良好。只需创建活动并复制您的广播接收器。在活动中选择一个按钮,然后按钮的 onCLick 调用您的 setAlarm 函数。

我能够在广播接收器中获取字符串。

这是我的代码。

 public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

                set_alarm(System.currentTimeMillis(), MainActivity.this, manager, "Time for tea");
            }
        });
    }


    static void set_alarm(long alarm_time_in_millis,Context cont,AlarmManager alarm_manager,String str)
    {
        Intent intent = new Intent(cont, to_call_when_alarm_goes_off.class);
        intent.putExtra("string_passed_in_bundle", str); 

        Log.i("xx","set_alarm ["+str+"]"); // The string I see in the Log is correct
        PendingIntent pIntent = PendingIntent.getBroadcast(cont,0, intent, 0);

        alarm_manager.cancel(pIntent);

        alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent);
    }
}

广播接收器和你的一样,所以我不把它放在这里。

最后在清单中声明广播接收器

输出

set_alarm [Time for tea]
to_call_when_alarm_goes_off: TIME TO WAKE UP!!! [Time for tea]
于 2012-07-26T05:29:07.483 回答
1

使用 theIntent而不是它的Bundle,所以...

str = arg1.getStringExtra("string");
于 2012-07-18T20:25:28.927 回答
1

您可能需要单步执行此代码并确保您获得的字符串是您所期望的。我敢打赌你没有得到你所期望的。

于 2012-07-18T15:39:44.647 回答
1

当您从

"string"

"alarm_string"

你还在 to_call_when_alarm_goes_off 类中得到一个字符串吗?或者你得到空?

如果您在此处删除 cancel(pIntent) 函数会发生什么:

alarm_manager.cancel(pIntent);

alarm_manager.set(AlarmManager.RTC_WAKEUP,alarm_time_in_millis, pIntent);

只是想减少噪音。“字符串”可能用于序列化或其他用途,它可能会让人感到困惑。更独特的价值也有帮助。

于 2012-07-19T15:13:16.823 回答
1

下面的代码对我有用。我通过 AndroidManifest 中带有意图过滤器的自定义意图调用 BroadcastReceiver。但是,我真的不能说为什么这会有所作为。话虽如此,下面的代码有效。

public class MainActivity extends Activity implements OnClickListener {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        setAlarm();
        break;
    }       
} 

private void setAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    long interval = System.currentTimeMillis() + 10000; // 10 seconds from now
    Intent launchIntent = new Intent();
    launchIntent.putExtra("your_key", "your string value");
    launchIntent.setAction(MyBroadcastReceiver.CUSTOM_INTENT);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP,interval,
            pendingIntent);     
}
}

和广播接收器:

public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String CUSTOM_INTENT = "com.your_custom_intent";

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    String message = bundle.getString("your_key");
    Log.d("test", message);
}
}

和 AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test2_2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

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

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

    <receiver
        android:name=".MyBroadcastReceiver">
        <intent-filter>
            <action android:name="com.your_custom_intent" />
        </intent-filter>
    </receiver>
</application>

</manifest>
于 2012-07-21T05:09:20.797 回答