[英]Not found activity to handle intent?
我正在編寫一個程序,當特定的短信到達手機時,我的應用程序中的主要活動應該被調用。 我已經注冊了一個BroadcastReceiver
並且調用該活動的意圖是在onReceive()
方法中。 問題是,每次我發送這個特定的 SMS 時,我都會關閉一個 Force。 閱讀 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;
}
}
嘗試像這樣改變你的<intent-filter>
:
<intent-filter>
<action android:name="edu.example.prankssms3.action.STARTMUSIC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
從廣播接收器啟動活動是一種不常見的操作。 嘗試將 FLAG_ACTIVITY_NEW_TASK 添加到用於 startActivity() 的 Intent。
從 Activity 上下文外部調用startActivity()
需要FLAG_ACTIVITY_NEW_TASK
標志。 希望這將解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.