最新文章:

首页 android

简单的服务开启产生随机数和关闭服务

发布时间:2016年05月10日 评论数:抢沙发 阅读数:260

    1.创建界面

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textView1"
            android:text="启动服务" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignRight="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="18dp"
            android:text="停止服务" />


    2.MainActivity

    package com.example.test;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    	static Handler handler = new Handler();
    	Button bt1 = null;
    	Button bt2 = null;
    	static TextView textView = null;
    	static String msTring;
    	static void update(String msg) {
    		msTring = msg;
    		handler.post(new Runnable() {
    			@Override
    			public void run() {
    				textView.setText(msTring);
    			}
    		});
    	}
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		bt1 = (Button) findViewById(R.id.button1);
    		bt2 = (Button) findViewById(R.id.button2);
    		textView = (TextView) findViewById(R.id.textView1);
    		bt1.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent(MainActivity.this, addService.class);
    				startService(intent);
    			}
    		});
    		bt2.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				Intent intent = new Intent(MainActivity.this, addService.class);
    				stopService(intent);
    			}
    		});
    	}
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		// Handle action bar item clicks here. The action bar will
    		// automatically handle clicks on the Home/Up button, so long
    		// as you specify a parent activity in AndroidManifest.xml.
    		int id = item.getItemId();
    		if (id == R.id.action_settings) {
    			return true;
    		}
    		return super.onOptionsItemSelected(item);
    	}
    }
    


    3.Service

    package com.example.test;
    
    import android.R.bool;
    import android.app.Service;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.IBinder;
    
    public class addService extends Service {
    	Thread thread = null;
    	boolean istop = true;
    	StringBuffer str = new StringBuffer();
    	Runnable runable = new Runnable() {
    		@Override
    		public void run() {
    			str.append("随机数为:");
    			for (int i = 0; i < 10 && istop == false; i++) {
    				str.append((int)(Math.random()*100)+" ");
    				MainActivity.update(str.toString());
    				try {
    					Thread.sleep(2000);
    				} catch (InterruptedException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			}
    		}
    	};
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	@Override
    	public void onCreate() {
    		thread = new Thread(runable);
    		super.onCreate();
    	}
    	@Override
    	public void onStart(Intent intent, int startId) {
    		thread.start();
    		istop = false;
    		super.onStart(intent, startId);
    	}
    	@Override
    	public void onDestroy() {
    		thread.interrupt();
    		istop = true;
    		super.onDestroy();
    	}
    }
    


二维码加载中...
本文作者:HDC      文章标题: 简单的服务开启产生随机数和关闭服务
本文地址:http://hdcin.cn/?post=56
版权声明:若无注明,本文皆为“小胖Blog's”原创,转载请保留文章出处。
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论