I already asked a question here :
combining-2-extended-activity-for-sms-notification
And now i get a new problem 😀
So i already make a nested Class like this :
public class SMSNotif extends Activity{
static final int HELLO_ID = 1;
BroadcastReceiver myReceiver = null;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
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";
}
Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
}
//Intent i = new Intent(SMSReceiver.this, SMSNotif.class);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//int icon = R.drawable.ic_launcher;
String tickerText = "Hello";
//long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());
//Context context = getApplicationContext();
String contentTitle = "My notification";
String contentText = "Hello World!";
Intent notificationIntent = new Intent(this, SMSNotif.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(HELLO_ID, notification);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
if(myReceiver != null){
unregisterReceiver(myReceiver);
myReceiver = null;
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if(myReceiver == null){
myReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter();
registerReceiver(myReceiver, filter);
}
}
}
And my MANIFEST is like this :
<activity
android:name=".SMSNotif"
android:label="@string/app_name" >
<receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</activity>
The problem is, my app can’t detect any new SMS now…
Where is my mistake?is it in my manifest?
I already tried changing my codes but i still can’t solve my problem…
So the question is :
Can you help me so my app can detect new SMS and make a notification everytime new SMS comes?
Thank you very much! 😀
And sory if i made some mistakes, English is not my native languange 🙂
I have an Idea : I tried to reverse the Class, so SMSReceiver will be the outer one and SMSNotif will be inside it…is it possible?(i tried it, and i got some errors) I think by inverse it the BroadcastReceiver will be able to detect new SMS..is it true?
You should specify that your receiver is in inner class in manifest.
The Receiver should not be in activity.
and your inner class SMSReceiver should be static
EDITED
If you want to just show the notification then Activity is not Required.