Android的广播接收器管理 [英] Android Broadcast receiver management

查看:158
本文介绍了Android的广播接收器管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用中,当接收短信我broacast开始。
我想,当我收到新短信时,第一个是完不成管理。
我有办法杀第proccess。
但是我不没有什么做。
如果你能帮助我。
我在Android的biginner。
还有就是我Brocast code。

感谢。

 进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsMessage;
进口android.text.TextUtils;公共类SMSReceiver扩展广播接收器{
私有静态最后的串号=15555215556;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    捆绑包= intent.getExtras();
    [对象]消息=(对象[])bundle.get(的PDU);
    SmsMessage [] =短信新SmsMessage [messages.length]    //创建
    的for(int i = 0; I< messages.length;我++){
        短信[I] = SmsMessage.createFromPdu((字节[])消息[I]);
    }
    对于(SmsMessage消息:SMS){        //Vérifie
        如果(TextUtils.equals(msg.getOriginatingAddress(),数)){
            意图I =新意图(背景下,DisplayActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            串SMS2 = msg.getMessageBody();
            i.putExtra(SMS,SMS2);
            context.startActivity(ⅰ);            abortBroadcast();
        }
    }}
}


解决方案

  

我想,当我收到新短信时,第一个是完不成管理。


这是不是很清楚地知道,你不应该做任何繁重的工作在你的广播,你最好复制必要的数据IE浏览器。共享preferences或数据库,并启动IntentService将进一步处理它。随着 IntentService 你可以通过将数据直接在到服务意向

但是,从您的code我没有看到任何长处理


  

杀第proccess


这始终是Android的下一个坏主意(看这里更长的解释:<一href=\"http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238\">Quitting一个应用程序 - 这是在皱起了​​眉头?)。也就是 abortBroadcast(); 你的意图?共进晚餐preSS通知?

In my Android application, when a receive a SMS my broacast start. I want to manage when i receive a new SMS when the first one is not finish. I have idea kill the first proccess. But i don't no what to do it. If you can help me. I'm biginner in Android. There is my Brocast code.

Thanks.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;

public class SMSReceiver extends BroadcastReceiver {
private static final String number = "15555215556";

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    // Creation
    for (int i=0; i < messages.length; i++) {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }
    for (SmsMessage msg : sms) {

        // Vérifie
        if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
            Intent i = new Intent(context, DisplayActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            String sms2 = msg.getMessageBody();
            i.putExtra("SMS",sms2);
            context.startActivity(i);

            abortBroadcast();
        }
    }

}
}

解决方案

I want to manage when i receive a new SMS when the first one is not finish.

This is not quite clear to me, you should not do any heavy work in your broadcast, you better copy necessary data to ie. shared preferences or database, and start IntentService that will process it further. With IntentService you can pass your data to service directly in Intent.

But from your code I dont see any long processing

kill the first proccess

That is always a bad idea under android (look here for longer explanation: Quitting an application - is that frowned upon?). Also is abortBroadcast(); your intention? to suppress notifications?

这篇关于Android的广播接收器管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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