如何在 Android 中使用警报管理器放入 Web 服务
How to put in webservice with alarm manager in Android
我不知道我将如何从 web 服务中放置数据,同时它会在警报管理器中工作以在每组时间发送数据。我知道如何从 web 服务中获取数据,但对于警报管理器我一无所知。我尝试从 web 服务中放入数据的过程,但它会出错,因为 class 扩展了 BroadcastReceiver。这就是我放入网络服务的方式。
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();
}
}
});
}
我如何使用警报管理器将数据放入 web 服务中?例如,这样我就可以每分钟从 WebService 发送数据。请帮忙。
更新自约瑟夫回答:
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();
}
}
});
}
}
接收者Class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, Web.class);
context.startService(i);
}
}
您可以创建一个 Service
,闹钟响起时应调用它。 Service
应该包含调用网络服务所需的所有代码。
为了重新安排对 Web 服务的调用,您可以使用 AlarmManager.setRepeating()
方法。
因此,以这种方式扩展 IntentService
(它是服务 class 的一个实现,可简化您的工作):
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
}
}
现在声明一个 BroadcastReceiver
应该接收
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context c, Intent arg1) {
Intent i = new Intent(c, DownloadService.class);
c.startService(i);
}
}
设置AlarmManager
重复操作。 (只调用一次)
Intent intent = new Intent(this, UpdateReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
alarmMgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis()+30000, AlarmManager.INTERVAL_HOUR * 2, alarmIntent);
在上面的示例中,将在 30 秒后调用 Web 服务,以后每隔 e 小时调用一次。
注意:如果您的设备重新启动,您的闹钟将被取消,因此如果您想重新创建它,您应该创建一个 BroadcastReceiver
来监听操作 android.intent.action.BOOT_COMPLETED
并允许你调用上面的代码来设置重复闹钟。
我找到了答案。在 AlarmReceiver 中初始化请求参数,然后删除 WebService 中的参数,然后放入来自另一个 class 的消息。这是代码。
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 服务中放置数据,同时它会在警报管理器中工作以在每组时间发送数据。我知道如何从 web 服务中获取数据,但对于警报管理器我一无所知。我尝试从 web 服务中放入数据的过程,但它会出错,因为 class 扩展了 BroadcastReceiver。这就是我放入网络服务的方式。
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();
}
}
});
}
我如何使用警报管理器将数据放入 web 服务中?例如,这样我就可以每分钟从 WebService 发送数据。请帮忙。
更新自约瑟夫回答:
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();
}
}
});
}
}
接收者Class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, Web.class);
context.startService(i);
}
}
您可以创建一个 Service
,闹钟响起时应调用它。 Service
应该包含调用网络服务所需的所有代码。
为了重新安排对 Web 服务的调用,您可以使用 AlarmManager.setRepeating()
方法。
因此,以这种方式扩展 IntentService
(它是服务 class 的一个实现,可简化您的工作):
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
}
}
现在声明一个 BroadcastReceiver
应该接收
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context c, Intent arg1) {
Intent i = new Intent(c, DownloadService.class);
c.startService(i);
}
}
设置AlarmManager
重复操作。 (只调用一次)
Intent intent = new Intent(this, UpdateReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
alarmMgr.setRepeating(AlarmManager.RTC, cal.getTimeInMillis()+30000, AlarmManager.INTERVAL_HOUR * 2, alarmIntent);
在上面的示例中,将在 30 秒后调用 Web 服务,以后每隔 e 小时调用一次。
注意:如果您的设备重新启动,您的闹钟将被取消,因此如果您想重新创建它,您应该创建一个 BroadcastReceiver
来监听操作 android.intent.action.BOOT_COMPLETED
并允许你调用上面的代码来设置重复闹钟。
我找到了答案。在 AlarmReceiver 中初始化请求参数,然后删除 WebService 中的参数,然后放入来自另一个 class 的消息。这是代码。
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.