Java 无法在android代码中的Alarm OnReceive方法上执行select.php
我在android应用程序中有MainActivity.java 因为我有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
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:inOnHandleContent(…)
您可以使用getApplicationContext()
要获取有效的上下文
@ashish:您是否已将服务添加到AndroidManifest.xml`文件中?