安卓activity界面,上面有两个按钮,一个是开始服务,一个是取消服务。
界面布局:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 17 18 <Button 19 android:id="@+id/quxiao" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_below="@+id/textView1" 23 android:layout_marginTop="65dp" 24 android:text="取消服务" /> 25 26 <Button 27 android:id="@+id/bangding" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_alignBaseline="@+id/quxiao" 31 android:layout_alignBottom="@+id/quxiao" 32 android:layout_alignParentRight="true" 33 android:layout_marginRight="66dp" 34 android:text="开始服务" /> 35 36 </RelativeLayout>
activity的代码:
1 package com.example.test2; 2 3 import java.util.Date; 4 5 import com.example.application.GlobalVaries; 6 7 import android.os.Bundle; 8 import android.os.Handler; 9 import android.os.IBinder; 10 import android.app.Activity; 11 import android.content.ComponentName; 12 import android.content.Intent; 13 import android.content.ServiceConnection; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 import android.widget.TextView; 18 import android.widget.Toast; 19 20 public class MainActivity extends Activity { 21 private boolean isBound = false; 22 TextView labelView; 23 private Intent serviceIntent; 24 private GlobalVaries global; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 global = (GlobalVaries) getApplication(); 31 labelView = (TextView) findViewById(R.id.textView1); 32 Button bindButton = (Button) findViewById(R.id.bangding); 33 Button unbindButton = (Button) findViewById(R.id.quxiao); 34 35 myMainRunning(); 36 // 绑定按钮 37 bindButton.setOnClickListener(new OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 if (!isBound) { 41 serviceIntent = new Intent(MainActivity.this, 42 MyService.class); 43 MainActivity.this.startService(serviceIntent); 44 isBound = true; 45 46 } 47 } 48 }); 49 // 取消绑定按钮 50 unbindButton.setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View v) { 53 onDestroy(); 54 System.exit(0); 55 finish(); 56 } 57 }); 58 59 } 60 61 private void myMainRunning() { 62 final Handler handler = new Handler(); 63 Runnable runnable = new Runnable(){ 64 @Override 65 public void run() { 66 // 在此处添加执行的代码 67 68 long nowTime = global.getNowTime(); 69 labelView.setText(""+new Date(nowTime)); 70 handler.postDelayed(this, 500);// 50是延时时长 71 } 72 }; 73 handler.postDelayed(runnable, 1000);// 打开定时器,执行操作 74 handler.removeCallbacksAndMessages(this);// 关闭定时器处理 75 } 76 77 @Override 78 protected void onDestroy() { 79 MainActivity.this.stopService(serviceIntent); 80 super.onDestroy(); 81 } 82 83 84 }
服务部分的代码:
1 package com.example.test2; 2 3 import java.util.Date; 4 5 import com.example.application.GlobalVaries; 6 7 import android.app.Service; 8 import android.content.Intent; 9 import android.os.Handler; 10 import android.os.IBinder; 11 import android.util.Log; 12 import android.widget.Toast; 13 14 public class MyService extends Service { 15 16 private GlobalVaries global; 17 18 @Override 19 public void onCreate() { 20 Log.i("start", "MyService onCreate"); 21 global = (GlobalVaries) getApplication(); 22 myrunningProgram(); 23 super.onCreate(); 24 } 25 26 private void myrunningProgram() { 27 final Handler handler = new Handler(); 28 Runnable runnable = new Runnable(){ 29 @Override 30 public void run() { 31 // 在此处添加执行的代码 32 Toast.makeText(MyService.this, ""+new Date(), 33 Toast.LENGTH_SHORT).show(); 34 global.setNowTime(System.currentTimeMillis()); 35 handler.postDelayed(this, 2000);// 50是延时时长 36 } 37 }; 38 handler.postDelayed(runnable, 1000);// 打开定时器,执行操作 39 handler.removeCallbacksAndMessages(this);// 关闭定时器处理 40 41 } 42 43 //开启绑定 44 @Override 45 public IBinder onBind(Intent arg0) { 46 //为了使Service支持绑定,需要在Service类中重载onBind()方法,并在onBind()方法中返回Service对象 47 Toast.makeText(this, "本地绑定:MyService", 48 Toast.LENGTH_SHORT).show(); 49 System.out.println("----2"); 50 51 return null; 52 } 53 54 //取消绑定 55 @Override 56 public boolean onUnbind(Intent intent) { 57 Toast.makeText(this, "取消本地绑定:MyService", 58 Toast.LENGTH_SHORT).show(); 59 System.out.println("----3"); 60 return false; 61 } 62 63 64 }
还用到了一个全局变量:
1 package com.example.application; 2 3 import android.app.Application; 4 5 public class GlobalVaries extends Application { 6 7 private long nowTime; 8 9 public long getNowTime() { 10 return nowTime; 11 } 12 13 public void setNowTime(long nowTime) { 14 this.nowTime = nowTime; 15 } 16 }
配置文件中需要对service和全局变量进行配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="com.example.application.GlobalVaries" android:theme="@style/AppTheme" > <activity android:name="com.example.test2.MainActivity" 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=".MyService"> <intent-filter android:name="com.example.test2.MyService"> </intent-filter> </service> </application> </manifest>
根据需求可以修改相应部分的代码。以上代码就是为了实现自己的需求。