asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
main activity java:
package com.asfman;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
 
public class main extends Activity {
    
/** Called when the activity is first created. */
    Button b1,b2,b3,b4;
    TestService mService;
    ProgressBar pb;
    MyReceiver receiver;
    TextView tv;
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1
=(Button)findViewById(R.id.button1);
        b2
=(Button)findViewById(R.id.button2);
        b3
=(Button)findViewById(R.id.button3);
        b4
=(Button)findViewById(R.id.button4);
        b1.setOnClickListener(l1);
        b2.setOnClickListener(l3);
        b3.setOnClickListener(l2);
        b4.setOnClickListener(l4);
        pb
=(ProgressBar)findViewById(R.id.progressBar1);
        tv
=(TextView)findViewById(R.id.txt);
    }

 
 
 
    
public class MyReceiver extends BroadcastReceiver {
        
//自定义一个广播接收器
        @Override
        
public void onReceive(Context context, Intent intent) {
            
// TODO Auto-generated method stub
            System.out.println("OnReceiver");
            Bundle bundle
=intent.getExtras();
            
int a=bundle.getInt("i");
            pb.setProgress(a);
            tv.setText(String.valueOf(a));
            
//处理接收到的内容
 
        }

        
public MyReceiver(){
            System.out.println(
"MyReceiver");
            
//构造函数,做一些初始化工作,本例中无任何作用
        }

 
    }

 
 
    OnClickListener l1
=new OnClickListener(){
 
        @Override
        
public void onClick(View v) {
            
// TODO Auto-generated method stub
            Log.i("info""--------------开始服务-------------------");
            startService(
new Intent(main.this, TestService.class));
            
//开始服务
        }

 
    }
;
    OnClickListener l2
=new OnClickListener(){
 
        @Override
        
public void onClick(View v) {
            
// TODO Auto-generated method stub
            Log.i("info""--------------结束服务-------------------");
            stopService(
new Intent(main.this, TestService.class));
            
//结束服务
        }

 
    }
;
    OnClickListener l3
=new OnClickListener(){
 
        @Override
        
public void onClick(View v) {
            
// TODO Auto-generated method stub
            
//注册接收器
            Log.i("info""--------------注册接收器-------------------");
            receiver
=new MyReceiver();
            IntentFilter filter
=new IntentFilter();
            filter.addAction(
"action.asfman");
            main.
this.registerReceiver(receiver,filter);
        }

 
    }
;
    OnClickListener l4
=new OnClickListener(){
 
        @Override
        
public void onClick(View v) {
            
// TODO Auto-generated method stub        
            Log.i("info""--------------解除注册接收器-------------------");
            main.
this.unregisterReceiver(receiver);
            
//解除注册接收器
        }

 
    }
;
}

service java
package com.asfman;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
 
public class TestService extends Service {
    
boolean isStop=false;
    @Override
    
public IBinder onBind(Intent intent) {
        
// TODO Auto-generated method stub
        Log.i("info""--------------onBind绑定-------------------");
        
return null;
    }

    
public void onCreate(){
        Log.i(
"info""--------------TestService onCreate-------------------");
        
super.onCreate();
    }

    
public void onStart(Intent intent,int startId){
        Log.i(
"info""--------------TestService onStart-------------------");
        
super.onStart(intent, startId);
        
new Thread(){//新建线程,每隔1秒发送一次广播,同时把i放进intent传出
            public void run(){
                
int i=0;
                
while(!isStop){
                    Intent intent
=new Intent();
                    intent.putExtra(
"i", i);
                    i
++;
                    Log.i(
"info""--------------TestService i:" +i+"-------------------");
                    intent.setAction(
"action.asfman");//action与接收器相同android.intent.action.asfman
                    sendBroadcast(intent);
                    Log.i(
"TAG",String.valueOf(i));
                    
try {
                        sleep(
1000);
                    }
 catch (InterruptedException e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }

        }
.start();
 
    }

    @Override
    
public void onDestroy() {
        Log.i(
"info""--------------TestService onDestroy-------------------");
        isStop
=true;//即使service销毁线程也不会停止,所以这里通过设置isStop来停止线程
        super.onDestroy();
    }

 
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package
="com.asfman"
      android:versionCode
="1"
      android:versionName
="1.0">
    
<uses-sdk android:minSdkVersion="7" />

    
<application android:icon="@drawable/icon" android:label="@string/app_name">
        
<activity android:name=".main"
                  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="TestService"></service>

    
</application>
</manifest>
posted on 2011-04-02 17:31 汪杰 阅读(332) 评论(0)  编辑 收藏 引用 所属分类: Java
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 466814
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜