我一直在尝试很多不同的方法来让这个演示运行,但我无法让它正常运行..
这是我的 Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messagekeeper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.messagekeeper.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReciever">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
这是我的 MainActivity 类(什么都不做):
package com.example.messagekeeper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是 SmsReciever 类:
package com.example.messagekeeper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
class SmsReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
Object[] pdus=(Object[])intent.getExtras().get("pdus");
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("SMSReceiver","SMS message sender: "+
shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: "+
shortMessage.getDisplayMessageBody());
}
}
当我收到一条消息时,我希望它在我的应用程序上看到它。在这种情况下,我的意思是当我使用此代码并在模拟器上运行我的应用程序并从一个模拟器向另一个模拟器发送消息时,我的应用程序崩溃并且我收到此错误: http: //puu.sh/1yZub