views:

694

answers:

3

I have a reciever that works well, but I can't seem to show a proper UI, although the toast appears correctly. As far as I can tell, this is caused by Android requiring the class to extend Activity, however, the class already extends BroadcastReciever, so I can't do this. So, I tried to do an Intent, but this failed too. There are no errors, but the screen doesn't show. Source code is below, and any help would be most appreciated.

Broadcast (Method in 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);
    }
}

Reciever 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();
    }

Manifest

<?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> 
}
A: 
Intent foo = new Intent(this, AlarmRing.class);
foo.putExtra("id", id);//example, if you wish to pass custom variables
this.startActivity(foo);

Edit
Check out this example to use BroadcastReciever within an Activity. http://almondmendoza.com/2009/01/04/getting-battery-information-on-android/

Pentium10
I tried that before, but i get errors on creator: The constructor Intent(Reciever, Class<AlarmRing>) is undefinedAs far as I can tell, its caused by the Reciever extending BroadcastReciever, not Activity.
Andy
try `this.getContext();`
Pentium10
+2  A: 

In a BroadcastReceiver onReceive() method, if you need a Context (e.g., to create an Intent), use the Context that is passed to you as a parameter of onReceive(). You even have this code in your onReceive() -- you're just not doing anything with the resulting Intent (e.g., calling startActivity()).

CommonsWare
RecieverV2 above, now just crashes the app when the reciever is called. It seems to want a flag of kinds, but im unsure how to set this up. LogCat: 03-21 23:05:45.573: ERROR/AndroidRuntime(769): java.lang.RuntimeException: Unable to start receiver com.comaad.andyroidalarm.Reciever: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Andy
Well, starting an activity from a broadcast receiver is an unusual operation. For most apps, popping up an activity unannounced will make the user very angry. An alarm clock app is one of the few that justify it. Follow the instructions in the error message -- add `FLAG_ACTIVITY_NEW_TASK` to the `Intent` you use for `startActivity()` -- and your problem will hopefully go away.
CommonsWare
A: 

you have not mentioned R.Layout.Alarm field anywhere.. so how will i call it. please help

nextcome
Firstly, your not meant to ask questions in replies, but anyway...I don't have this field you talk of, so am rather confused.If you mean, how to get from one CLASS to next CLASS then thats what INTENT is for. Remember that activities must be defined in the manifest file too.
Andy