0
我想以固定的时间间隔从服务器获取通知,因此我已将服务器响应设置为服务。所以,基本上我使用服务来运行后台并创建通知。我的问题是我收到的通知不是固定的时间间隔。那么如何以固定的时间间隔获取通知。即使重新启动手机,我也想继续我的服务(获取通知)。我有点困惑,如果手机将被关闭,那么我不得不重新启动我的服务。如果我必须重新启动它,那我该如何处理。您的帮助将不胜感激。提前致谢。通知和服务在Android
Alert_notifications.java
public class Alert_notifications extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_notifications);
Button buttonStartService = (Button)findViewById(R.id.startservice);
Button buttonStopService = (Button)findViewById(R.id.stopservice);
buttonStartService.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Alert_notifications.this, com.example.gpstracking.NotifyService.class);
Alert_notifications.this.startService(intent);
}});
buttonStopService.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(NotifyService.ACTION);
intent.putExtra("RQS", NotifyService.STOP_SERVICE);
sendBroadcast(intent);
}});
}
}
NotifyService.java
public class NotifyService extends Service {
final static String ACTION = "NotifyServiceAction";
final static String STOP_SERVICE = "";
final static int RQS_STOP_SERVICE = 1;
private static final String url_Weather_details1="http://198.168.1.2/Weatherforecast1/";
private static final String TAG_SUCCESS = "success";
NotifyServiceReceiver notifyServiceReceiver;
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
@Override
public void onCreate() {
// TODO Auto-generated method stub
notifyServiceReceiver = new NotifyServiceReceiver();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
JSONParser jsonParser = new JSONParser();
params.add(new BasicNameValuePair("LAT", "LAT"));
params.add(new BasicNameValuePair("LONGITUDE", "LONG"));
Log.d("params", params.toString());
// getting weather details by making HTTP request
// Note that weather details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(url_Weather_details1,
"GET", params);
// check your log for json response
Log.d("Weather Details", json.toString());
// json success tag
int success = 0;
try {
success = json.getInt(TAG_SUCCESS);
System.out.println("success"+success);
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (success == 2) {
// successfully received weather details
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION);
registerReceiver(notifyServiceReceiver, intentFilter);
// Send Notification
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.ic_launcher,"Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Heavy Rain!";
String notificationText = "";
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(
getBaseContext(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,
notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(notifyServiceReceiver);
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class NotifyServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == RQS_STOP_SERVICE) {
stopSelf();
}
}
}
}
如果您正在从服务器发出通知,则可以为其设置cronjob并设置/计划时间间隔。请让我知道,如果这个想法有帮助,或者你需要更多的澄清。 – sUndeep 2015-02-10 09:08:36