Chinaunix首页 | 论坛 | 博客
  • 博客访问: 547814
  • 博文数量: 179
  • 博客积分: 3845
  • 博客等级: 中校
  • 技术积分: 2003
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-16 21:25
文章分类
文章存档

2012年(74)

2011年(105)

分类: 嵌入式

2011-07-21 23:18:21

代码清单:

ServiceDemoAct.java
  1. package foolstudio.demo;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;

  8. public class ServiceDemoAct extends Activity implements OnClickListener {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main_view);
  14.         
  15.         Button btnStart = (Button)findViewById(R.id.BTN_START);
  16.         Button btnStop = (Button)findViewById(R.id.BTN_STOP);
  17.         btnStart.setOnClickListener(this);
  18.         btnStop.setOnClickListener(this);
  19.     }

  20.     @Override
  21.     //按钮被点击时回调
  22.     public void onClick(View v) {
  23.         // TODO Auto-generated method stub
  24.         switch(v.getId() ) {
  25.             case R.id.BTN_START: { //点击启动服务按钮
  26.                 doStart();
  27.                 break;
  28.             }
  29.             case R.id.BTN_STOP: {  //点击停止服务按钮
  30.                 doStop();
  31.                 break;
  32.             }            
  33.         }
  34.     }
  35.     
  36.     //启动服务操作
  37.     private void doStart() {
  38.         Intent startService = new Intent(this, DummyService.class);
  39.         this.startService(startService);
  40.     }
  41.     
  42.     //停止服务操作
  43.     private void doStop() {
  44.         Intent startService = new Intent(this, DummyService.class);
  45.         this.stopService(startService);        
  46.     }
  47. };
DummyService.java
  1. package foolstudio.demo;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.util.Log;
  6. import android.widget.Toast;


  7. public class DummyService extends Service {

  8.     @Override
  9.     public IBinder onBind(Intent intent) {
  10.         // TODO Auto-generated method stub
  11.         return null;
  12.     }

  13.     @Override
  14.     public void onCreate() {
  15.         // TODO Auto-generated method stub
  16.         super.onCreate();
  17.         
  18.         Log.d(getClass().getName(), "Service created.");
  19.     }

  20.     @Override
  21.     public void onStart(Intent intent, int startId) {
  22.         // TODO Auto-generated method stub
  23.         super.onStart(intent, startId);
  24.         
  25.         Toast.makeText(this, "I'm a service!", Toast.LENGTH_LONG).show();
  26.         
  27.         Log.d(getClass().getName(), "Service starting...");
  28.     }

  29.     @Override
  30.     public void onDestroy() {
  31.         // TODO Auto-generated method stub
  32.         super.onDestroy();
  33.         
  34.         Log.d(getClass().getName(), "Service destroyed.");
  35.     }
  36. };
AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.       package="foolstudio.demo"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".ServiceDemoAct"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.      <service android:label="DummyService" android:name=".DummyService">
  15.             <intent-filter>
  16.                 <action android:name="foolstudio.demo.DummyService" />
  17.             </intent-filter>
  18.         </service>
  19.     </application>
  20.     <uses-sdk android:minSdkVersion="3" />
  21. </manifest>
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent" >
  6.     <TextView
  7.      android:layout_width="fill_parent"
  8.      android:layout_height="wrap_content"
  9.      android:text="@string/app_name"
  10.      android:gravity="center_horizontal"
  11.      android:padding="10sp"
  12.      android:textSize="8pt"
  13.      android:textColor="#00FF00" />
  14.     <Button android:id="@+id/BTN_START"
  15.      android:layout_width="wrap_content"
  16.      android:layout_height="wrap_content"
  17.      android:layout_gravity="center_horizontal"
  18.      android:text="Start service" />
  19.     <Button android:id="@+id/BTN_STOP"
  20.      android:layout_width="wrap_content"
  21.      android:layout_height="wrap_content"
  22.      android:layout_gravity="center_horizontal"
  23.      android:text="Stop service" />    
  24. </LinearLayout>






阅读(623) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

tianbianfei2011-07-22 18:05:10

哎,看不懂楼主的代码啊,刚学习android,请问一下楼主 android都有哪些方式存储数据呢?可否到下面这个链接帮着解决一下,谢谢!http://doumiw.com/market/community/t!showTopic.do?topicId=33