我正在编写一个程序,当特定的短信到达手机时,应该调用我的应用程序中的主要活动。我已经注册了一个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;
}
}