我一直试图在我的onCreate方法中让一个闹钟管理员调用我的班级BatteryPost
:
BatteryPost.java:
public class BatteryPost extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
HttpClient httpclient = new DefaultHttpClient();
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String batteryText = prefs.getString("battery", null);
String id = prefs.getString("id", null);
String URL = "http://mysite.com/checkin/index.php?device_uid="+id+"&bat="+batteryText;
{
try{
String uri = URL.replace(" ", "%20"); //replace spaces with %20
Log.d("Checkin", uri);
HttpPost httppost = new HttpPost(uri); //post object
@SuppressWarnings("unused")
HttpResponse response = httpclient.execute(httppost); //execution
} catch (ClientProtocolException e) {
Log.d("Checkin", "ClientProtocolException");
} catch (IOException i) {
Log.d("Checkin", "IOException");
}
}
}
}
我的onCreate()中应该被AlarmManager击中:
public void onCreate(Bundle savedInstanceState) {
/*
* Our onCreate block...
*/
super.onCreate(savedInstanceState);
Context context = getApplicationContext();
setContentView(R.layout.activity_main);
Button submit = (Button)findViewById(R.id.submitButton);
submit.setOnClickListener(submitListener); //Initialization of the view and objects
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);
Intent intent = new Intent(context, BatteryPost.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 600000, sender);
}
当我按下按钮时,我注册了手动更新电池电量到服务器,因此值正确存储,但是AlarmManager无法每分钟调用一次BatteryPost(只需每分钟进行测试,之后会更长) )。
我一直在寻找SO上的其他帖子试图找到类似的代码,但实现这一点已经变得棘手了。
感谢。
答案 0 :(得分:1)
我注意到的第一件事是你要检查600000
毫秒。这不是60秒,而是600,或者换句话说,10分钟。这可能是问题吗?