80,349
社区成员
发帖
与我相关
我的任务
分享
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent it=super.getIntent();
String strShopURL=it.getStringExtra("shop_url");
String StrUID=it.getStringExtra("UID");
String StrPWD=it.getStringExtra("PWD");
Intent intent=new Intent(MainActivity.this,OrderService.class);
String url2=new StringBuilder(strShopURL).append(this.getString(R.string.message_url))
.append("?UID=").append(StrUID).append("&PWD=").append(StrPWD).toString();
intent.putExtra("URL", url2);
startService(intent);
}
public class OrderService extends Service{
public static final String SETTING_FILE="Config";
public static final String SETTING_UID="UID";
public static final String SETTING_PWD="PWD";
private boolean threadDisable;
private NotificationManager notificationManager;
private SharedPreferences settings;
private String strURL;
@Override
public IBinder onBind(Intent intent){
return null;
}
@Override
public void onCreate(){
super.onCreate();
}
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
strURL = intent.getStringExtra("URL");
Log.d("Service_URL",strURL);
notificationManager=(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
new Thread(new Runnable(){
@Override
public void run(){
while(!threadDisable){
try{
Log.d("Begin_try",strURL);
String StrTmp=getOrders(strURL);
if (!StrTmp.equals("")){
Notification notification2=new Notification();
String ringName=RingtoneManager.getActualDefaultRingtoneUri(OrderService.this, RingtoneManager.TYPE_NOTIFICATION ).toString();
notification2.sound=Uri.parse(ringName);
notificationManager.notify(0,notification2);
Notification notification1=new Notification();
notification1.icon=R.drawable.ic_launcher;
notification1.tickerText=StrTmp;
PendingIntent intent=PendingIntent.getActivity(OrderService.this,0, new Intent(OrderService.this,MainActivity.class), 0);
notification1.setLatestEventInfo(OrderService.this, "通知", "您收到:"+StrTmp+" 个新订单", intent);
notificationManager.notify(0,notification1);
}
Thread.sleep(60000);
}catch(InterruptedException e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
}).start();
}
@Override
public void onDestroy(){
super.onDestroy();
this.threadDisable=true;
}
private String getOrders(String strURL){
String res="";
try{
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGetRequest=new HttpGet(strURL);
Log.d("BeginGet","BeginGet");
HttpResponse response=httpClient.execute(httpGetRequest);
Log.d("EndGet",response.getStatusLine().toString());
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
String responseMsg=EntityUtils.toString(response.getEntity());
res=responseMsg;
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
return res;
}
}