So, i want to create an app that can give it's user a notif everytime a SMS come. My main problem is i get confuse how to combining my 2 Acitivites/classes. I extends BroadcastReceiver in SMSReceiver class so it can detect if any new SMS come, and i extends Activity in my SMSNotif class. The problem is I CAN'T EXTENDS MORE THAN 1 CLASS in each activity.
This is SMSReceiver class :
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);
}
}
And this is my SMSNotif class :
public class SMSNotif extends Activity{
static final int HELLO_ID = 1;
@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);
}
}
Again, my main question is : how to combine those Activities so i everytime the user get a new SMS, my app will show it's notif(not just a toast form SMSNotif).
Here is the trick to solve your problem:
Make the BroadcastReceiver-class local to the Activity-class. This way the receiver can do something in the activity
you also have to (un-)register the receiver in the activities-code