Android:从Application Class启动新活动

我有一个 Android应用程序,从应用程序类播放音频.我的应用程序类中有一个PhoneStateListener,它可以在有电话时暂停音频.

我想在通话结束时开始一项特定的活动,但我无法做到.这是我的代码:

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
.....
.....
}

如果有人能告诉我如何以正确的方式开始活动,我将不胜感激.

谢谢!

解决方法

好的,所以我知道你已经找到了另一种解决方案,但是我正在解决这个问题并发现了一些对我有用的东西.我没有调用一个intent,而是使用了pendingIntent,一个intent过滤器和一个待定帖子.这是一个代码snippit,其他人有这个问题.
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();
    }

然后在您的清单中,确保为捕获活动设置了intent过滤器

<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>

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...