有时候,Android的广播接收器不工作 [英] Sometimes Android BroadcastReceiver does not work

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

问题描述

阅读了大量关于SOF与功放的话题后,博客文章中,我已经得到了广播接收器工作。但是,我已经注意到有时这是行不通的,特别是当我在电话。我只是想知道,是否广播接收器,如果我得到短信-之间的呼叫工作?或者有什么是错在code。

After reading a lot of topics on SOF & blog post, I have got the BroadcastReceiver working. But I have noticed sometime it does not work, specially when I am on call. I just want to know, does BroadcastReceiver work if I get SMS in-between the call ? or there is something is wrong in the code.

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

    Log.d("SmsReceiver Broadcast", "OK");

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        Log.d("Network is connected. Executing TheTask()", "OK");

    new TheTask().execute("http://somedomain.tld/index.php?userId=12345678");

    }

    if (networkInfo == null) {

        Log.d("Network is NOT connected.", "FAIL");
    }

}

class TheTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg0) {
        String text = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(arg0[0]);
            HttpResponse resp = httpclient.execute(httppost);
            HttpEntity ent = resp.getEntity();
            text = EntityUtils.toString(ent);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return text;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        String tocheck = "Some text";
        if(result.contains(tocheck))
        {
            Log.d("string contains some text", result);
        }
    }
}
}

的Andr​​oidManifest.xml

        <receiver android:name=".SmsReceiver" android:enabled="true" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

我使用3G网络。这可能是由于网络断开。如果是这样,有什么解决办法执行的AsyncTask时,我的手机又获得数据网络?

I am using 3G network. It may be due to network disconnects. If so, is there any workaround to execute AsyncTask when my phone again get data network ?

编辑:

裹在的AsyncTask isConnected 。现在,如果手机没有连接到互联网和放大器;我收到短信的的AsyncTask 将不会被执行。在这种情况下,我想使用 ScheduledExecutorService的安排 AsynckTask 在接下来的20分钟内5分钟的时间间隔要执行4次。我将高度有义务任何人都可以帮我在这。

Wrapped the AsyncTask into isConnected. Now if the phone is not connected to Internet & I get SMS the AsyncTask will not be executed. In that situation I want to use ScheduledExecutorService to schedule AsynckTask to be executed 4 times at the interval of 5 minutes within next 20 minutes. I will be highly obliged anyone can help me in this.

推荐答案

因为你没有提供任何堆栈跟踪我不能肯定的断开。但我pretty确保SMS_RECEIVED广播作用正在播出的全系统当您收到的短信。现在,如果你没有收到短信,由于连接不良或根本是完全合乎逻辑的无连接的系统不触发,因为没有收到短信的SMS_RECEIVED行动。

I cannot be sure about the disconnects since you haven't provided any stack trace. But I am pretty sure the SMS_RECEIVED broadcast action is being broadcasted system-wide when you receive sms. Now if you do not receive SMS due to bad connection or no connection at all it's perfectly logical for the system not to trigger an SMS_RECEIVED action since no SMS was received.

至于

有没有什么解决办法执行的AsyncTask时,我的手机又获得
  数据网络

is there any workaround to execute AsyncTask when my phone again get data network

您可以实现另一个广播接收器将侦听网络改变但这似乎并没有这样一个好主意。你应该尝试重现该问题并为您在您的堆栈跟踪任何异常。

you can implement another Broadcast Receiver that will listen for network changes but that doesn't seem such a good idea. You should try and reproduce the problem and check for any exceptions in your stack trace.

是什么是错在code。

is something is wrong in the code.

您接收器似乎在你的清单中正确注册,
的onReceive()方法似乎要正确启动的AsyncTask
doInBackground 似乎没有出现任何问题和 onPostExecute (虽然官方例子似乎忽略超任何调用)似乎就好了。

Your receiver seems to be properly registered in your manifest, the onReceive() method seems to be properly launching the AsyncTask, the doInBackground doesn't seem to have any problem and onPostExecute (although the official example seems to omit any call to super) seems to be just fine.

结论:尝试重现该问题,并从堆栈跟踪收集数据,以什么可能导致错误(网络开销可能是一个原因,但这只是猜测)

Conclusion: Try to reproduce the problem and gather data from the stack trace as to what might cause the error (network overhead might be one the causes but that's just speculation).

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

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