无法在安卓代码中的Alarm OnReceive方法上执行select.php


unable to execute select.php on Alarm OnReceive method in android code

我在android应用程序中有MainActivity.java

因为我有setRepeatingAlarm(),它在每个120sec之后执行,并执行select.php,检查表中是否有可用的名称。

public void setRepeatingAlarm() {
    Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+120000,
    (120000), pendingIntent);
}

广播接收器:

public class TimeAlarm extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        //get select query data
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.myweb.com/select.php");
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();   
            if(is != null) {
                Intent notificationIntent = new Intent(context, MainActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                 NotificationManager notificationManager = (NotificationManager) context
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                 Notification noti = new NotificationCompat.Builder(context)
                                    .setSmallIcon(R.drawable.ic_launcher)
                                    .setTicker("mytestapp")
                                    .setContentTitle("mytestapp")
                                    .setContentText("Please check new update!")
                                    .setContentIntent(contentIntent)
                                    //At most three action buttons can be added
                                    .setAutoCancel(true).build();
                int notifyID =0;
                notificationManager.notify(notifyID, noti);
            }
            is.close();
        } catch(Exception e) {
        }
    }
}

但我无法执行CCD_ 3进入catch块。没有得到任何错误详细信息:(

select.php提供类似{"name":"mynametest"}null 的输出

建议我这里出了什么问题

根据我对您问题的评论,您可以将网络代码移动到IntentService中,只需从BroadcastReceiver开始即可。IntentService中的所有工作都由运行onHandleIntent(...)方法的工作线程处理,因此在其中执行网络任务是安全的。

IntentService:

public class MyIntentService extends IntentService {
    // You must have a parameter-less ctor for an IntentService
    public MyIntentService() {
        super("MyIntentService");
    }
    public MyIntentService(String name) {
        super(name);
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        // This method is run on a separate worker thread so it's
        // safe to put all of your network code here
    }
}

将您当前在BroadcastReceiver onReceive(...)方法中的所有内容复制到IntentServiceonHandleIntent(...)方法中,并将其替换为代码以启动服务,如下所示。。。

public class TimeAlarm extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, MyIntentService.class);
        context.startService(i);
    }
}