结合 2 个扩展活动进行短信通知:D

发布于 2025-01-08 18:55:31 字数 2294 浏览 0 评论 0原文

所以,我想创建一个应用程序,每次收到短信时都可以向用户发出通知。 我的主要问题是我很困惑如何组合我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2025-01-15 18:55:31

这是解决您的问题的技巧:

将 BroadcastReceiver 类设置为 Activity 类的本地类。这样,接收者可以在活动中执行某些操作,

public class SMSNotif extends Activity{

    class SMSReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            ...
            someActivityMetod("Hello world from receiver");
        }

    }

    void someActivityMetod(String message)
    {
        ...
    }
}

您还必须在活动代码中(取消)注册接收者

public class SMSNotif extends Activity{

            BroadcastReceiver myReceiver = null;



    @Override
    public void onPause()
    {

        if (myReceiver != null)
        {
            unregisterReceiver(myReceiver);
            myReceiver = null;
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

        if (myReceiver == null)
        {
            myReceiver = new SMSReceiver();
            IntentFilter filter = new IntentFilter("Some.Action.Code");
            registerReceiver(myReceiver, filter);
        }
    }

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

public class SMSNotif extends Activity{

    class SMSReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            ...
            someActivityMetod("Hello world from receiver");
        }

    }

    void someActivityMetod(String message)
    {
        ...
    }
}

you also have to (un-)register the receiver in the activities-code

public class SMSNotif extends Activity{

            BroadcastReceiver myReceiver = null;



    @Override
    public void onPause()
    {

        if (myReceiver != null)
        {
            unregisterReceiver(myReceiver);
            myReceiver = null;
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

        if (myReceiver == null)
        {
            myReceiver = new SMSReceiver();
            IntentFilter filter = new IntentFilter("Some.Action.Code");
            registerReceiver(myReceiver, filter);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文