我的 android 项目中有 3 个类:
- 启动.java
- 短信接收器.java
- 开始活动.java
Launch.java 如下:-
package alertApp;
import com.alertapp.R;
import android.app.Activity;
import android.os.Bundle;
public class Launch extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
}
}
SmsReceiver 类使用广播接收器来访问传入消息并向 StartActivity 类触发意图。
编辑:这是 SmsReceiver 类:
package alertApp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends BroadcastReceiver {
//String number = "+919561356345";
String number = "LM-WAYSMS";
String sender, body;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
if(extras == null)
return;
Object[] pdus = (Object[]) extras.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender = SMessage.getOriginatingAddress().toString();
body = SMessage.getMessageBody().toString();
Toast.makeText(context, "From : " +sender + "\n Message : " +body, Toast.LENGTH_LONG).show();
}
if(sender.contentEquals(number)){
Intent open = new Intent(context, StartActivity.class);
open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
open.putExtra("body", body);
open.putExtra("number", number);
context.startActivity(open);
}
}
}
编辑:更新清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alertapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.alertapp.Launch"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.alertapp.StartActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.testapp.NextActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver
android:name="com.alertapp.SmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
android:enabled="true">
<intent-filter>
<action android:name = "android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
当我通过连接手机运行程序时,它显示了致命的异常错误。
07-02 19:41:46.512: E/AndroidRuntime(10003): FATAL EXCEPTION: main
07-02 19:41:46.512: E/AndroidRuntime(10003): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.alertapp/com.alertApp.Launch}: java.lang.ClassNotFoundException: com.alertApp.Launch
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.os.Looper.loop(Looper.java:137)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-02 19:41:46.512: E/AndroidRuntime(10003): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 19:41:46.512: E/AndroidRuntime(10003): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 19:41:46.512: E/AndroidRuntime(10003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-02 19:41:46.512: E/AndroidRuntime(10003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-02 19:41:46.512: E/AndroidRuntime(10003): at dalvik.system.NativeStart.main(Native Method)
07-02 19:41:46.512: E/AndroidRuntime(10003): Caused by: java.lang.ClassNotFoundException: com.alertApp.Launch
07-02 19:41:46.512: E/AndroidRuntime(10003): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-02 19:41:46.512: E/AndroidRuntime(10003): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-02 19:41:46.512: E/AndroidRuntime(10003): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
07-02 19:41:46.512: E/AndroidRuntime(10003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
07-02 19:41:46.512: E/AndroidRuntime(10003): ... 11 more