结合 2 个扩展活动进行短信通知:D
所以,我想创建一个应用程序,每次收到短信时都可以向用户发出通知。 我的主要问题是我很困惑如何组合我的 2 个活动/课程。我在 SMSReceiver 类中扩展了 BroadcastReceiver,以便它可以检测是否有任何新短信到来,并且我在 SMSNotif 类中扩展了 Activity。问题是我不能在每个活动中扩展超过 1 类。
这是 SMSReceiver 类:
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);
}
}
这是我的 SMSNotif 类:
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);
}
}
同样,我的主要问题是:如何组合这些活动,以便每次用户收到新短信时,我的应用程序都会显示它的 notif(而不仅仅是 SMSNotif 的 toast 形式) 。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是解决您的问题的技巧:
将 BroadcastReceiver 类设置为 Activity 类的本地类。这样,接收者可以在活动中执行某些操作,
您还必须在活动代码中(取消)注册接收者
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