我正在尝试从Broadcast Receiver类启动一个活动。 这是MainActivity.java
package com.wissen.sms;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这是SMSReceiver.java
package com.wissen.sms.receiver;
import com.wissen.sms.Nwact;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
/**
* The class is called when SMS is received.
*/
public class SMSReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
//show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
//and then start an activity
Intent i = new Intent(context,Nwact.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
这是清单文件: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wissen.sms"
android:versionCode="1"
android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
<activity android:name=".newact"></activity>
<receiver android:name=".receiver.SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
但是当我运行时我在日志文件中收到错误: -
08-31 10:44:38.011: ERROR/AndroidRuntime(749): Uncaught handler: thread main exiting due to uncaught exception
08-31 10:44:38.031: ERROR/AndroidRuntime(749): java.lang.RuntimeException: Unable to start receiver com.wissen.sms.receiver.SMSReceiver: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.wissen.sms/com.wissen.sms.Nwact}; have you declared this activity in your AndroidManifest.xml?
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2646)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ActivityThread.access$3100(ActivityThread.java:119)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.os.Looper.loop(Looper.java:123)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at java.lang.reflect.Method.invoke(Method.java:521)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at dalvik.system.NativeStart.main(Native Method)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.wissen.sms/com.wissen.sms.Nwact}; have you declared this activity in your AndroidManifest.xml?
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ApplicationContext.startActivity(ApplicationContext.java:555)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.content.ContextWrapper.startActivity(ContextWrapper.java:248)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at com.wissen.sms.receiver.SMSReceiver.onReceive(SMSReceiver.java:40)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2637)
08-31 10:44:38.031: ERROR/AndroidRuntime(749): ... 10 more
请说明问题发生的位置。
答案 0 :(得分:1)
是否在manifest中添加exported = true的答案,因为您的活动应该可以由另一个应用程序(此处为系统)调用?
http://developer.android.com/guide/topics/manifest/receiver-element.html#exported
此致 斯特凡