结合2短信通知扩展活动:D [英] Combining 2 extended Activity for SMS Notification :D

查看:126
本文介绍了结合2短信通知扩展活动:D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想创建一个应用程序,可以给它的用户notif每次短信来了。
我的主要问题是,我得到混淆如何结合我的2个活动还/班。我SMSReceiver类扩展广播接收器,因此它可以检测到是否有新的短信来了,我在SMSNotif类扩展活动。问题是我不能在每一个活动来延长超过1类。

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.

这是SMSReceiver类:

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);
}

}

这是我SMSNotif类:

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);

}

}

此外,我的主要问题是:如何将这些活动结合起来,所以我每次用户获得新的短信,我的应用程序将显示它的notif(不只是一个形式敬酒SMSNotif)

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

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)
    {
        ...
    }
}

您还需要(非)寄存器中的活动 - code接收器

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);
        }
    }

这篇关于结合2短信通知扩展活动:D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆