【问题标题】:Not found activity to handle intent?没有找到处理意图的活动?
【发布时间】:2012-11-07 19:05:29
【问题描述】:

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

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

【问题讨论】:

    标签: android android-intent broadcastreceiver android-activity


    【解决方案1】:

    尝试像这样更改您的&lt;intent-filter&gt;

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我的 Intent 已经有了 FLAG_ACTIVITY_NEW_TASK 标志。显然它没有帮助
      • 我在意图过滤器中添加了以下行,就像 Nikolai Samteladze 所说的那样,它起作用了。
      猜你喜欢
      • 2011-08-18
      • 2013-08-24
      • 2022-11-20
      • 2015-12-03
      • 1970-01-01
      • 2015-09-04
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多