Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Starting new Activity from Application Class

I have an android app that plays audio from the application class. I have a PhoneStateListener in my application class that pauses the audio when there is a phone call.

I want to start a particular activity when the call ends, but I am unable to. here is my code:

public void getPhoneState(){

TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        if (state == TelephonyManager.CALL_STATE_RINGING) {
            if(audio.isPlaying())
               audioPlayer.pause();

        } 
            else if(state == TelephonyManager.CALL_STATE_IDLE) {

                audio.start();
                Intent missintent= new Intent(context,AudioActivity.class);
                missintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(missintent);


        } 
            else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {

            if(audio.isPlaying())
            audioPlayer.pause();

        }
        super.onCallStateChanged(state, incomingNumber);


    }
};

if(mgr != null) {
    mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}

public boolean handleAudio(String source, int id) {

phoneState();
//Code for Playing Audio
.....
.....
}

I would appreciate it if someone could show me how to start the activity in the correct manner.

Thanks!

like image 494
RagHaven Avatar asked Jun 20 '12 16:06

RagHaven


1 Answers

Ok so I know you found another solution already, but I was cracking around at it and found something that worked for me. Instead of calling an intent I used pendingIntent, an intent filter, and pending post. Here is a code snippit for anyone else out there having this issue.

Context context = MyApplication.this.getApplicationContext();
Intent errorActivity = new Intent("com.error.activity");//this has to match your intent filter
errorActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 22, errorActivity, 0);
try {
    pendingIntent.send();
    } 
catch (CanceledException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    }

Then in your manifest just make sure you set the intent filter for the catching activity

<activity
    android:name="UncaughtErrorDialogActivity"
    android:theme="@android:style/Theme.Dialog" >
    <intent-filter>
        <action android:name="com.error.activity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
like image 56
MikeIsrael Avatar answered Sep 24 '22 15:09

MikeIsrael