公共类MyService扩展了Service {
int a=0;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
StrictMode.enableDefaults();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
getData();
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
public void getData(){
String result="";
InputStream isr=null;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://192.168.96.1/php1/noti.php");
HttpResponse response=httpclient.execute(httpPost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}catch (Exception e){
Log.e("Log_tag", "Error in Http Connection" + e.toString());
Toast.makeText(this, "Could not connect to database", Toast.LENGTH_LONG).show();
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.icon, MyText, System.currentTimeMillis());
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(isr,"US-ASCII"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
isr.close();
result=sb.toString();
}catch (Exception e){
Log.e("Log_tag","Error converting Result"+e.toString());
}
try {
String s="";
JSONArray jArray=new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json=jArray.getJSONObject(i);
s=s +
"State: "+json.getString("state")+"\n";
String MyNotificationTitle = "E-Door Security";
String MyNotificationText = "Door Opened!!!";
Intent MyIntent = new Intent(Intent.ACTION_VIEW);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(), 0, MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
点击。 FLAG_CANCEL_CURRENT标志取消pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID, mNotification);
a=0;
}
}catch (Exception e){
if(a!=1) {
String MyNotificationTitle = "E-Door Security";
String MyNotificationText = "door Closed!!!";
Intent MyIntent = new Intent(Intent.ACTION_VIEW);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(), 0, MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
点击。 FLAG_CANCEL_CURRENT标志取消pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID, mNotification);
a=1;
}
}
}
}
答案 0 :(得分:0)
使用AlarmManager调用您的服务:
Intent serviceIntent = new Intent(this, ServiceClass.class);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,
serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setRepeating(AlarmManager.RTC_WAKEUP, 0,2000, pendingIntent);
这里的2000是时间段,这意味着onStartCommand()每2秒被调用一次。