activity service receiver 的应用

 

package com.ye.HttpClientTest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HttpClientTest extends Activity implements Button.OnClickListener {
    /** Called when the activity is first created. */
protected static final int GUIUPDATEIDENTIFIER = 0x101;
public TextView accept;
public Button send;
public EditText showEditText;
public ProgressBar progressBar;
public String urlString,dataString;
private String msg="";

public class HttpClientReceiver extends BroadcastReceiver{
//接收广播
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.v("HttpClientTest", "onReceive");
progressBar.setVisibility(View.GONE);
msg = intent.getStringExtra("msg");
accept.setText(msg);

}
     }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    Log.i("HttpClientTest", "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        accept= (TextView)findViewById(R.id.accept);
        send = (Button) findViewById(R.id.send);
        showEditText = (EditText) findViewById(R.id.show);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setVisibility(View.GONE);
        send.setOnClickListener(this);
       
        //注册广播器
        IntentFilter filter=new IntentFilter("com.ye.service.msg");
        HttpClientReceiver receiver=new HttpClientReceiver();
        registerReceiver(receiver,filter);
       
    }

@Override
public void onClick(View v) {

// TODO Auto-generated method stub
Log.i("HttpClientTest", "onClick()");
progressBar.setVisibility(View.VISIBLE);
urlString = "http://api.showji.com/locating/?m="+showEditText.getText()+"&output=text";
Intent intent = new Intent("com.ye.HttpClientTest.HttpClientTestService");
Bundle bundle = new Bundle();
bundle.putString("url", urlString);
intent.putExtras(bundle);
intent.setClass(HttpClientTest.this, HttpClientTestService.class);

startService(intent);
}
}

package com.ye.HttpClientTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class HttpClientTestService extends Service {

public String resultString = "";
public String urlString = "";
String websiteData = null;
DefaultHttpClient client;
Intent intent;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.v("HttpClientTestService", "in  onStart()");
Bundle bundle = intent.getExtras();
urlString = bundle.getString("url");
new Thread(mRunnable).start();

}
Runnable mRunnable = new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
Log.i("Thread", "Thread is start");
resultString = getUrlData(urlString);
sendMsg(resultString);

}
};

//onCreate在service初始化的时候被调一次,直到service生命周期的结束
@Override
public void onCreate() {
Log.i("HttpClientTestService", "in  onCreate()");
client = new DefaultHttpClient();
intent =  new Intent("com.ye.service.msg");
}


public String getUrlData(String url){  
    try {   
    URI uri = new URI(url);  
    HttpGet method = new HttpGet(uri);  
    HttpResponse res = client.execute(method);  
    InputStream data = res.getEntity().getContent(); 
    websiteData = generateString(data);
    } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace(); 
    } catch (URISyntaxException e) {
    // TODO Auto-generated catch block 
    e.printStackTrace();
    } 
    return websiteData;
    }

public String generateString(InputStream stream) { 
InputStreamReader reader = new InputStreamReader(stream); 
BufferedReader buffer = new BufferedReader(reader);  
StringBuilder sb = new StringBuilder(); 
try { 
String cur;
while ((cur = buffer.readLine()) != null) { 
sb.append(cur + "\n");
}
} catch (IOException e) { 
// TODO Auto-generated catch block
e.printStackTrace();

try {
stream.close();
buffer.close();
} catch (IOException e) { 
// TODO Auto-generated catch block 
e.printStackTrace();

return sb.toString(); 
}


// 发送广播信息
    private void sendMsg(String msg){
    Log.i("HttpClientTestService", "sendMsg()");
        // 指定广播目标的 action (注:指定了此 action 的 receiver 会接收此广播)
       
        // 需要传递的参数
        intent.putExtra("msg", msg);
        // 发送广播
        this.sendBroadcast(intent);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ye.HttpClientTest"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
   
        <activity android:name=".HttpClientTest"
        android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<service android:name="HttpClientTestService">
            <intent-filter>
                <action android:name="com.ye.HttpClientTest.HttpClientTestService"/>
            </intent-filter>
   </service>
    </application>
   
<uses-permission android:name="android.permission.INTERNET" />

</manifest>

你可能感兴趣的:(apache,thread,android,.net,OS)