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

时间:2014-10-07 14:26:11

标签: java php android

我在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) {

        }
    }
}
  

但是我无法执行HttpResponse response = httpclient.execute(httppost);进入catch块。没有得到任何错误细节:(

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

的输出

建议我这里有什么问题

1 个答案:

答案 0 :(得分:1)

继续我对您的问题的评论后,您可以将网络代码移至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(...)方法中当前拥有的所有内容复制到onHandleIntent(...)的{​​{1}}方法中,并将其替换为代码以启动服务,如下所示...

IntentService