找不到处理意图的活动?

时间:2012-11-07 19:05:30

标签: android android-intent broadcastreceiver android-activity

我正在编写一个程序,当特定短信到达手机时,应该调用我的应用程序中的主要活动。我已经注册了BroadcastReceiver,并且调用活动的意图是onReceive()方法。问题是,每次我发送这个特定的短信,我都会收到一个强制关闭。阅读logcat,我看到以下NullPoint异常:

10- 20 02:07:16.558: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to    start receiver edu.example.prankssms3.SMSReceiverActivity3: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=edu.example.prankssms3.action.STARTMUSIC flg=0x10000000 }

但就我而言,一切都做得对。有人请告诉我问题在哪里吗?提前谢谢。

这是清单文件:

<activity
        android:name=".MainActivity3"
        android:label="@string/title_activity_main_activity3" >
        <intent-filter>                
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="edu.example.prankssms3.action.STARTMUSIC" />
        </intent-filter>
    </activity>

    <receiver android:name="SMSReceiverActivity3">
        <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>

这里是广播接收者

public void onReceive(Context context, Intent intent) {
        // get the message passed in
        Bundle bdl = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if(bdl != null){
            // retrieve the sms message received
            Object[] pdus = (Object[]) bdl.get("pdus");         
            msgs = new SmsMessage[pdus.length];
            for(int i=0; i<msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                str += msgs[i].getMessageBody().toString();
            }
            if(str.equals("FIRE_THE_MISSILES")){
                // The pranking comes here
                // start the activity to play the music and send sms    message
                Intent launchActivity = new Intent();
                launchActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                launchActivity.setAction("edu.example.prankssms3.action.STARTMUSIC");
                context.startActivity(launchActivity);
            }
        }
    }

,这里是主类,也是意图调用的类:

public class MainActivity3 extends Activity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main3, menu);
        return true;
    }
}

2 个答案:

答案 0 :(得分:3)

尝试像这样更改<intent-filter>

<intent-filter>
    <action android:name="edu.example.prankssms3.action.STARTMUSIC" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

答案 1 :(得分:0)

从广播接收器中拉取活动是不常见的操作。尝试将FLAG_ACTIVITY_NEW_TASK添加到您用于startActivity()的Intent中。

从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。希望这能解决您的问题。