我有一个checkboxlist,用户选择了一些项目,然后我开始选择json格式随后我从alarmManager中触发json字符串,然后尝试将其发送到GetLLRD
IntentService类但我面临问题我我没有在GetLLRD类中获得意图,因为我没有获得GetLLRD类中的任何输出。
我该如何解决?
MainActivity中的代码:
Intent intentJson = new Intent(MainActivity.this, GetLLRD.class);
intentJson.putExtra("json_data", json);
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 3, intentJson, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
5 * 1000, pintent);
System.out.println("test intentJson output: " +intentJson);
startService(intentJson);
GetLLRD课程:
public class GetLLRD extends IntentService {
public GetLLRD(String name) {
super(name);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String jSONString = intent.getStringExtra("json_data");
System.out.println("test json is " + jSONString);
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
String jSONString = intent.getStringExtra("json_data");
System.out.println("Test" + jSONString);
if(jSONString != null){
System.out.println("Test");
}
}
}
答案 0 :(得分:1)
你必须在未决意图中注册广播接收者。你可以 通过使用service.for创建待处理的意图做错了 从警报管理器我们需要使用广播接收器。
注册AlarmManager
int REQUEST_CODE= 0;
Calendar calendar = Calendar.getInstance();
int hour = 13;
int minute = 00;
calendar.set(Calendar.HOUR_OF_DAY, hour);//24 Hour format
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 00);
/*calendar.set(Calendar.AM_PM, peried.equalsIgnoreCase("AM") ? Calendar.AM : Calendar.PM);*/
Intent myIntent = new Intent(context, MyReceiver.class);
myIntent.putExtra("json_data", json);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
5 * 1000, pendingIntent );
<强> 接收机 强>
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getStringExtra("json_data");
new ShowToast(context, action);
if (action.length() > 1) {
startService(context, action);
}
} catch (Exception e) {
}
}
public void startService(Context context, String action) {
Intent service1 = new Intent(context, GetLLRD.class);
service1.putExtra("json_data", action);
context.startService(service1);
}
}
在清单中注册接收器并请求使用AlarmManager权限。
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".MyReceiver" />