I declare an intent in my manifest :
<activity
android:name=".GetResponse"
android:label="@string/title_activity_main"
android:launchMode="singleTask">
<intent-filter>
<action android:name="ir.smspeik.sms.getresponse" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and then create a class name GetResponse :
package ir.smspeik.sms;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class GetResponse extends Activity{
IntentFilter intentFilter;
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//---display the SMS received in the TextView---
//TextView SMSes = (TextView) findViewById(R.id.textView1);
Toast.makeText(context, intent.getExtras().getString("sms"),
Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getresponse);
intentFilter = new IntentFilter();
intentFilter.addAction("SMS_RECEIVED_ACTION");
//---register the receiver---
registerReceiver(intentReceiver, intentFilter);
}
}
and a broadcaster :
package ir.smspeik.sms;
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 ReceiveSms extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//---launch the MainActivity---
Intent mainActivityIntent = new Intent(context, GetResponse.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//---send a broadcast to update the SMS received in the activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
}
but when I receive a message application raise an error :
08-08 19:40:40.513: E/AndroidRuntime(692): FATAL EXCEPTION: main
08-08 19:40:40.513: E/AndroidRuntime(692): java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{ir.smspeik.sms/ir.smspeik.sms.GetResponse}:
java.lang.NullPointerException
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread.access$600(ActivityThread.java:130)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.os.Handler.dispatchMessage(Handler.java:99)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.os.Looper.loop(Looper.java:137)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread.main(ActivityThread.java:4745)
08-08 19:40:40.513: E/AndroidRuntime(692): at
java.lang.reflect.Method.invokeNative(Native Method)
08-08 19:40:40.513: E/AndroidRuntime(692): at
java.lang.reflect.Method.invoke(Method.java:511)
08-08 19:40:40.513: E/AndroidRuntime(692): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-08 19:40:40.513: E/AndroidRuntime(692): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 19:40:40.513: E/AndroidRuntime(692): at
dalvik.system.NativeStart.main(Native Method)
08-08 19:40:40.513: E/AndroidRuntime(692): Caused by:
java.lang.NullPointerException
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:378)
08-08 19:40:40.513: E/AndroidRuntime(692): at
ir.smspeik.sms.GetResponse.<init>(GetResponse.java:34)
08-08 19:40:40.513: E/AndroidRuntime(692): at
java.lang.Class.newInstanceImpl(Native Method)
08-08 19:40:40.513: E/AndroidRuntime(692): at
java.lang.Class.newInstance(Class.java:1319)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-08 19:40:40.513: E/AndroidRuntime(692): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
08-08 19:40:40.513: E/AndroidRuntime(692): ... 11 more
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en