I am trying to start an activity from Broadcast Receiver class. This is 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);
}
}
This is the 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);
}
}
This is the Manifest file :-
<?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>
But when i Run i am getting the error in log file :-
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
Please specify where the problem is occuring.
Was the answer to add exported = true in manifest as your activity should be callable by another app (here the system) ?
http://developer.android.com/guide/topics/manifest/receiver-element.html#exported
Regards, Stéphane
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.