我想使用Alarm Manager将数据发送到我的parse SDK数据库。
没有Alarm Manager,我也能够发送数据。
我的下面的代码不能正确工作,因为parse SDK数据库没有显示更新的数据。
这是我的代码:
Main Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_todo);
setTitle(R.string.create_todo);
alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute(new String[]{});
}
class MyAsyncTask extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... params) {
try {
ParseObject parseObject = new ParseObject("Todo");
parseObject.put("name", "abc");
parseObject.save();
}
catch(ParseException e)
{
}
return null;
}
}
}
ParseException
的catch
中添加一些记录器,以防它抛出错误。 - Barun