Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法在android代码中的Alarm OnReceive方法上执行select.php_Java_Php_Android - Fatal编程技术网

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

Java 无法在android代码中的Alarm OnReceive方法上执行select.php,java,php,android,Java,Php,Android,我在android应用程序中有MainActivity.java 因为我有setRepeatingAlarm(),它在每120秒之后执行一次,并执行select.php,检查表中是否有可用的名称 public void setRepeatingAlarm() { Intent intent = new Intent(MainActivity.this, TimeAlarm.class); PendingIntent pendingIntent = PendingIntent.ge

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

因为我有
setRepeatingAlarm()
,它在每
120秒之后执行一次
,并执行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)进入捕捉块。未获取任何错误详细信息:(

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


根据我对您的问题的评论,建议我这里有什么问题。

您可以将您的网络代码移动到一个
IntentService
中,只需从您的
广播接收器开始即可。
IntentService
中的所有工作都由运行
OnHandleContent(…)的工作线程处理
方法,以便在其中安全地执行网络任务

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(…)
方法中拥有的所有内容复制到您的
IntentService
onHandleContent(…)
方法中,并将其替换为启动服务的代码,如下所示

public class TimeAlarm extends BroadcastReceiver {

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

检查logcat。很可能您得到的是
NetworkOnMainThreadException
。原因是
onReceive(…)
方法在主(UI)上运行
BroadcastReceiver
线程,您不允许在该线程上运行网络操作。我建议您使用
IntentService
来执行您想要执行的操作,只需在
BroadcastReceiver
onReceive
方法中启动即可。很好,这会导致错误
“主线程中的工作太多”
你能帮我看看如何在我上面的代码中使用
onReceive
中的
IntentService
吗……我对android非常陌生。谢谢你的帮助,给我几分钟时间,我会用一些基本代码添加一个答案。什么是
startService(i)
方法在哪里?:/it'it's is give me error said
方法startService类型TimeAlarm创建方法未定义(Intent)
@ashish:Sorry-可能应该是
context.startService(i)
protectedvoid onHandleIntent(Intent-Intent)
当我在进一步的代码中使用上下文时,我需要正确地指定上下文,但它使类变得抽象,就像这样`公共抽象类MyIntentService扩展IntentService`@ashish:in
OnHandleContent(…)
您可以使用
getApplicationContext()
要获取有效的
上下文
@ashish:您是否已将服务添加到AndroidManifest.xml`文件中?