从Android BroadcastReciever获取UI

时间:2010-03-21 20:02:49

标签: java android broadcastreceiver

我有一个运行良好的接收器,但我似乎无法显示正确的UI,尽管吐司正确显示。据我所知,这是由Android要求类扩展Activity引起的,但是,该类已经扩展了BroadcastReciever,所以我不能这样做。 所以,我试图做一个Intent,但这也失败了。没有错误,但屏幕没有显示。源代码如下,任何帮助都将非常受欢迎。

广播(AndyRoidAlarm中的方法)

public void setAlarm(){
    Intent intent = new Intent(AndyRoidAlarm.this, Reciever.class);
    PendingIntent sender = PendingIntent.getBroadcast(AndyRoidAlarm.this,
                0, intent, 0);

    // We want the alarm to go off 30 seconds from now.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);

    // Schedule the alarm!
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

    // Tell the user about what we did.
    if (mToast != null) {
        mToast.cancel();
    }
    mToast = Toast.makeText(AndyRoidAlarm.this, "Alarm Scheduled for 30secs", Toast.LENGTH_LONG);
    mToast.show();
}

Reciever

public class Reciever extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Alarm Recieved", Toast.LENGTH_LONG).show();
        Intent i = new Intent();
        i.setClass(context, AlarmRing.class);
    }
}

接收者V2

@Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "Alarm Recieved", Toast.LENGTH_LONG).show();
        Intent foo = new Intent(context, AlarmRing.class);
        //foo.putExtra("id", "id");//example, if you wish to pass custom variables
        context.startActivity(foo);
    }

AlarmRing

public class AlarmRing extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm);

        MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.sweetchild);
        mp.start();
    }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.comaad.andyroidalarm"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndyRoidAlarm"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.comaad.andyroidalarm.Reciever" android:enabled="true">
            <intent-filter>
                <action android:name="com.comaad.andyroidalarm.Reciever"></action>
            </intent-filter>
        </receiver>
        <activity android:name=".AlarmRing"></activity>
    </application>
</manifest> 
}

2 个答案:

答案 0 :(得分:3)

BroadcastReceiver onReceive()方法中,如果您需要Context(例如,创建Intent),请使用传递给Context的{​​{1}}你作为onReceive()的参数。您甚至在onReceive()中都有此代码 - 您只是对结果Intent没有做任何事情(例如,呼叫startActivity())。

答案 1 :(得分:0)

Intent foo = new Intent(this, AlarmRing.class);
foo.putExtra("id", id);//example, if you wish to pass custom variables
this.startActivity(foo);

修改
查看此示例以在Activity中使用BroadcastReciever。 http://almondmendoza.com/2009/01/04/getting-battery-information-on-android/