问题
I already asked a question here : combining-2-extended-activity-for-sms-notification
And now i get a new problem :D
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! :D 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?
回答1:
You should specify that your receiver is in inner class in manifest.
<activity
android:name=".SMSNotif"
android:label="@string/app_name" >
</activity>
<receiver android:name="your.package.name.SMSNotif$SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
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.
public class SMSReceiver extends BroadcastReceiver {
private static final int HELLO_ID = 0;
@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();
// String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) arg0
.getSystemService(Context.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(arg0, SMSNotif.class);
PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0,
notificationIntent, 0);
notification.setLatestEventInfo(arg0, contentTitle, contentText,
contentIntent);
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(HELLO_ID, notification);
}
}
}
回答2:
I am not sure it will solve your problem or not,but I think,you should try making following changes to your manifest file:
<activity
android:name=".SMSNotif"
android:label="@string/app_name" >
</activity>
<receiver android:name="com.your.path.SMSNotif$SMSReceiver" >
<intent-filter android:priority="100" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
I hope,it will solve your problem.Because the broadcast receiver is inner class of SMSNotif class so you will have to give full path along with ActivityName$RecevierName as i shown above.
EDIT :
I have added to intent-filter tag(see in above code).this will notify your app before it will notify android os for new sms.
来源:https://stackoverflow.com/questions/9465434/using-2-nested-class-to-get-notification-from-new-sms-d