报警设置和后台任务
Alarm setting and background task
场景如下:我的应用程序执行到 mysql 数据库的连接,另一个用户在该数据库中写入数据。我的应用程序只有在必要时才能连接到数据库,因此用户激活一个监听器,如果在服务器端,数据库包含特定数据,发出通知并停止监听。我已经实现了提供通知的功能和后台服务,但我不知道如何通过asynctask获取数据发送到php文件,因为发送到asynctask查询的数据不一样但每次都改变。所以我想,当用户发送第一个请求时,应用程序将要发送的数据写入首选项文件,因此每次后台服务进行新调用时,它都会从首选项中读取参数以发送到文件 php。
还有其他解决方案吗?
有人可以帮我解释每次闹钟激活服务时如何调用首选项吗?
我用过这个代码:
在主要活动中
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
报警接收器class
public class AlarmReceiver extends BroadcastReceiver{
SharedPreferences sharedPreferences;
String userid;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service Ok", Toast.LENGTH_SHORT).show();
//Only to know that it was tested and works properly!
// Here I should read preferences from preference file and
// call asynctask that receives json result. If it's 1 it
// call a procedure for notification and then erase data
// and stops service, otherwise it wait for a new call
// every 5 minutes
}
}
抱歉我的长post,我希望得到你的帮助。
提前致谢。
您可以在 android,
中使用 HttpClient
查看示例post 到服务器调用
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("username", "test_user"));
nameValuePair.add(new BasicNameValuePair("password", "123456789"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
别忘了提一下
<uses-permission android:name="android.permission.INTERNET" />
这是 sharedpreference 调用
SharedPreferences sharedpreferences = getSharedPreferences("myPreference", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
获取字符串
String value = preferences.getString("key", "DEFAULT");
在onreceive 中调用共享偏好如下
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
}
场景如下:我的应用程序执行到 mysql 数据库的连接,另一个用户在该数据库中写入数据。我的应用程序只有在必要时才能连接到数据库,因此用户激活一个监听器,如果在服务器端,数据库包含特定数据,发出通知并停止监听。我已经实现了提供通知的功能和后台服务,但我不知道如何通过asynctask获取数据发送到php文件,因为发送到asynctask查询的数据不一样但每次都改变。所以我想,当用户发送第一个请求时,应用程序将要发送的数据写入首选项文件,因此每次后台服务进行新调用时,它都会从首选项中读取参数以发送到文件 php。
还有其他解决方案吗?
有人可以帮我解释每次闹钟激活服务时如何调用首选项吗?
我用过这个代码:
在主要活动中
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
报警接收器class
public class AlarmReceiver extends BroadcastReceiver{
SharedPreferences sharedPreferences;
String userid;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Service Ok", Toast.LENGTH_SHORT).show();
//Only to know that it was tested and works properly!
// Here I should read preferences from preference file and
// call asynctask that receives json result. If it's 1 it
// call a procedure for notification and then erase data
// and stops service, otherwise it wait for a new call
// every 5 minutes
}
}
抱歉我的长post,我希望得到你的帮助。 提前致谢。
您可以在 android,
中使用 HttpClient查看示例post 到服务器调用
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("username", "test_user"));
nameValuePair.add(new BasicNameValuePair("password", "123456789"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
// write response to log
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
} catch (IOException e) {
// Log exception
e.printStackTrace();
}
别忘了提一下
<uses-permission android:name="android.permission.INTERNET" />
这是 sharedpreference 调用
SharedPreferences sharedpreferences = getSharedPreferences("myPreference", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
获取字符串
String value = preferences.getString("key", "DEFAULT");
在onreceive 中调用共享偏好如下
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = context.getSharedPreferences("myPreference",
Context.MODE_PRIVATE);
}