Java 如何在安卓系统中加入带有报警管理器的webservice

Java 如何在安卓系统中加入带有报警管理器的webservice,java,android,web-services,alarmmanager,Java,Android,Web Services,Alarmmanager,我不知道我将如何从webservice中输入数据,同时它将在alarm manager中工作,以每套时间发送数据。我知道如何从webservice中放入数据,但对于alarm manager,我没有任何想法。我尝试从webservice中放入数据,但出现了一个错误,因为该类具有扩展的BroadcastReceiver。这就是我在Web服务中的设置方式 public void passdata(View View){ String a = name.getText().toString(); Re

我不知道我将如何从webservice中输入数据,同时它将在alarm manager中工作,以每套时间发送数据。我知道如何从webservice中放入数据,但对于alarm manager,我没有任何想法。我尝试从webservice中放入数据,但出现了一个错误,因为该类具有扩展的BroadcastReceiver。这就是我在Web服务中的设置方式

public void passdata(View View){
String a = name.getText().toString();
RequestParams params = new RequestParams();

if(a != null){
params.put("name", a);
WebService(params);
}
}

public void WebService(RequestParams params) {
        progressDialog.show();

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://192.168.8.100:8080/taxisafe3/webService/login", params, new AsyncHttpResponseHandler() {
            public void onSuccess(String response) {
                progressDialog.hide();
                try {
                    JSONObject object = new JSONObject(response);

                    if (object.getBoolean("status")) {
                        Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
                        finish();
                        gotoHome();
                    } else {
                        Toast.makeText(getApplicationContext(), "Username or Password is incorrect!", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(int statusCode, Throwable error, String content) {
                progressDialog.hide();

                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                } else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Unexpected Error Occured! (No Internet Connection)", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
如何使用alarm manager将数据放入webservice?例如,我可以每分钟从Web服务发送数据。请帮忙

更新自Joseph答案:

    public class Web extends IntentService {
    String msg = "aw";
    public Web(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        RequestParams params = new RequestParams();
        params.put("message", msg);
        WebService(params);

        Intent in = new Intent(Web.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Web.this, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(calendar.MILLISECOND, 1);
        alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000 * 60 * 1, pendingIntent);
    }
    public void WebService(RequestParams params) {

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://192.168.254.105:8080/taxisafe3/webService/emergency", params, new AsyncHttpResponseHandler() {
            public void onSuccess(String response) {
                try {
                    JSONObject object = new JSONObject(response);

                    if (object.getBoolean("status")) {
                        Toast.makeText(getApplicationContext(), "Emergency Sent to Server", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Emergency not Sent to Server", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onFailure(int statusCode, Throwable error, String content) {

                if (statusCode == 404) {
                    Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                } else if (statusCode == 500) {
                    Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Unexpected Error Occured! (No Internet Connection)", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
接收级

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {        
        Intent i = new Intent(context, Web.class);
        context.startService(i);
}
}

您可以创建一个
服务
,当警报响起时应调用该服务。
服务
应该包含调用web服务所需的所有代码。 为了重新安排对web服务的调用,可以使用方法

因此,通过以下方式扩展
IntentService
(它是服务类的一个实现,简化了您的工作):

public class DownloadService extends IntentService {

  public DownloadService () {
      super("DownloadService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
      callWS(); //this method can execute your AsyncTask for calling web service, or anything you want
  }
}
现在声明一个应接收

public class UpdateReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context c, Intent arg1) {
        Intent i = new Intent(c, DownloadService.class);
        c.startService(i);     
    }
}
设置
AlarmManager
以重复该操作。(仅此一次)

在上面的示例中,对web服务的调用将在30秒后完成,并且在未来的每个e小时内完成


注意:如果您的设备重新启动,您的警报将被取消,因此,如果你想重新创建它,你应该创建一个
BroadcastReceiver
,监听操作
android.intent.action.BOOT\u COMPLETED
,并允许你调用上面的代码来设置重复报警。

这是我在我的一个应用程序中使用的,工作非常好。我已经用它在服务器上张贴每15分钟的数据

public class BackgroundService extends Service {

        public static final String ACTION_PING = "com.example.ACTION_PING";
        public static final String ACTION_CONNECT = "com.example.ACTION_CONNECT";
        public static final String ACTION_SHUT_DOWN ="com.example.ACTION_SHUT_DOWN";

        private final static String TAG = "BackgroundService";
        static Context context;
        private static volatile PowerManager.WakeLock lockStatic = null;

        long INTERVAL =AlarmManager.INTERVAL_FIFTEEN_MINUTES;

    public static Intent startIntent(Context context) {
    Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_CONNECT);
            return i;
    }

        public static Intent pingIntent(Context context) {
            Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_PING);
            return i;
        }

        public static Intent closeIntent(Context context) {
            Intent i = new Intent(context, BackgroundService.class);
            i.setAction(ACTION_SHUT_DOWN);
            return i;
        }

        synchronized private static PowerManager.WakeLock getLock(Context context) {
            if (lockStatic == null) {
                PowerManager mgr = (PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE);

                lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                lockStatic.setReferenceCounted(true);
            }

            return (lockStatic);
        }



        @Override
        public void onCreate() {
            super.onCreate();
            Log.d(TAG, "onCreate");
    context = this;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.i(TAG, "onDestroy");
        }

        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.i(TAG, "onStart");

        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Log.i(TAG, "onStartCommand");

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock lock = getLock(this.getApplicationContext());

            if (!lock.isHeld()) {
                lock.acquire();
            }

            if (intent != null) {
                if (ACTION_SHUT_DOWN.equals(intent.getAction())) {
                    stopSelf();
                    return super.onStartCommand(intent, flags, startId);
                }
            }

            if (intent == null || (intent.getAction() != null && !intent.getAction().equals(ACTION_SHUT_DOWN))) {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                PendingIntent operation = PendingIntent.getService(this, 0,
                BackgroundService.pingIntent(this),
                PendingIntent.FLAG_NO_CREATE);
                if (operation == null) {
                PendingIntent operation = PendingIntent.getService(this, 0, BackgroundService.pingIntent(this), PendingIntent.FLAG_UPDATE_CURRENT);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pm.isScreenOn() ? SCREEN_ON_INTERVAL : SCREEN_OFF_INTERVAL, operation);
                }
            }

            //fetch parsms and make your web service call here

            return START_STICKY;
        } 
使用此选项启动服务

startService(BackgroundService.startIntent(context));
 AlarmManager am = (AlarmManager)
                context.getSystemService(Context.ALARM_SERVICE);
                 PendingIntent operation = PendingIntent.getService(context, 0,
                 BackgroundService.pingIntent(context),
                PendingIntent.FLAG_NO_CREATE);
                 if (operation != null) {
                 am.cancel(operation);
                 operation.cancel();
                }
                context.startService(BackgroundService.closeIntent(context));
使用此选项可终止服务

startService(BackgroundService.startIntent(context));
 AlarmManager am = (AlarmManager)
                context.getSystemService(Context.ALARM_SERVICE);
                 PendingIntent operation = PendingIntent.getService(context, 0,
                 BackgroundService.pingIntent(context),
                PendingIntent.FLAG_NO_CREATE);
                 if (operation != null) {
                 am.cancel(operation);
                 operation.cancel();
                }
                context.startService(BackgroundService.closeIntent(context));

我找到了答案。在AlarmReceiver中,初始化请求参数,然后删除Web服务中的参数,然后放置来自另一个类的消息。这是代码

public class AlarmReceiver extends BroadcastReceiver {
    RequestParams params = new RequestParams();
    @Override
    public void onReceive(final Context context, Intent intent)
    {
//This is the message come from another class
String msg = bundle.getString("mess");

//check if the message is not null
        if(PatternChecker.isNotNull(msg)) {
            params.put("message",msg);
            Webservice();
        }
}

WebService method here to perform the sending data to the webservice.

先生,我把报警管理器放在哪里?在调用web服务之前,我将把上面代码中的params.put放在哪里?您可以通过以下方式在intent中传递参数:
intent.putExtra(“EXTRA_params”,params)注意:您的类RequestParams应实现
Parcelable
接口)报警管理器将在DownloadService中设置?在调用Web服务时?当您想要启动服务时,应调用AlarmManager。例如,当你点击一个按钮,或者第一次启动你的应用程序时。先生,我更新了问题,并输入了我根据你的建议创建的代码。我是对的?因为当我试着运行它的时候。它仍然不会从服务器发送数据。我可以;i don’我没有从数据库中看到新的报告。谢谢你的回答。但是我需要将数据发布到webservice,webservice将完成其余的工作,以将我发布的数据保存到数据库中。这是我的问题。关于如何执行将向webservice发送数据的请求参数,因为我无法将向webservice发布数据的方法放在BroadcastReceiver内部。这是唯一的问题。将webservice方法放在BroadcastReceiver中,我认为它已经可以工作了。报警管理器将从WebService发送数据您的请求参数的来源是什么…用户输入..?否。我得到了用户的位置,并添加了字符串,以使其他用户阅读消息时更加愉快。然后在收到该消息后,我将其放在Web服务上,这就是为什么它有请求参数。我已经通过分析代码解决了这个问题。谢谢你的评论。