如何在AlarmReceiver.java中使用GetNotifications?
我对java/android中的代码有一个小问题 当前正在MainActivity中使用此代码:如何在AlarmReceiver.java中使用GetNotifications?,java,android,Java,Android,我对java/android中的代码有一个小问题 当前正在MainActivity中使用此代码: private class GetNotifications extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInB
private class GetNotifications extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(ApiJsonNotificationsURL);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray notifications = jsonObj.getJSONArray("notifications");
for (int i = 0; i < notifications.length(); i++) {
JSONObject n = notifications.getJSONObject(i);
int id = (i);
String title = n.getString("title");
String message = n.getString("message");
String image = n.getString("image");
//notification
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
if(image.equals("null")){
mNotificationManager.showSmallNotification(id, title, message, intent);
}else{
mNotificationManager.showBigNotification(id, title, message, image, intent);
}
//notification
}
} catch (final JSONException e) {
/*runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});*/
}
} else {
/*runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});*/
}
return null;
}
}
如何在AlarmReceiver中使用新的GetNotifications().execute():
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "here i want use GetNotifications", Toast.LENGTH_SHORT).show();
}
}
我是JAVA的初学者,不知道怎么做。
这几天我一直在努力。
它将错误返回到“上下文”。
有人能帮我吗?您可以在
AlarmReceiver
中使用相同的AsyncTask
,也可以像
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "here i want use GetNotifications", Toast.LENGTH_SHORT).show();
new GetNotifications(context).execute();
}
private class GetNotifications extends AsyncTask<Void, Void, Void> {
private Context context;
public GetNotifications(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
return null;
}
}
}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
公共类AlarmReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(最终上下文、意图){
Toast.makeText(上下文,“我想在这里使用GetNotifications”,Toast.LENGTH_SHORT.show();
新建GetNotifications(context.execute();
}
私有类GetNotifications扩展异步任务{
私人语境;
公共GetNotifications(上下文){
this.context=上下文;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
返回null;
}
}
}
使用
onReceive
方法中的Context
。谢谢@sanjay kakadiya!我不知道如何使用上下文
。现在它正在工作。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "here i want use GetNotifications", Toast.LENGTH_SHORT).show();
new GetNotifications(context).execute();
}
private class GetNotifications extends AsyncTask<Void, Void, Void> {
private Context context;
public GetNotifications(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
return null;
}
}
}