标签:
1 package com.example.metrox.l14; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.content.ServiceConnection; 6 import android.os.IBinder; 7 import android.support.v7.app.AppCompatActivity; 8 import android.os.Bundle; 9 import android.view.View; 10 11 public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection { 12 13 Intent intent; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 intent = new Intent(MainActivity.this,MyService.class); 19 // findViewById(R.id.btnStartService).setOnClickListener(new View.OnClickListener() { 20 // @Override 21 // public void onClick(View view) { 22 // startService(intent); 23 // } 24 // }); 25 26 // findViewById(R.id.btnStopService).setOnClickListener(new View.OnClickListener() { 27 // @Override 28 // public void onClick(View view) { 29 // stopService(intent); 30 // System.out.println("服务已停止!!!"); 31 // } 32 // }); 33 findViewById(R.id.btnStartService).setOnClickListener(this); 34 findViewById(R.id.btnStopService).setOnClickListener(this); 35 findViewById(R.id.btnBindService).setOnClickListener(this); 36 findViewById(R.id.btnUnBindService).setOnClickListener(this); 37 } 38 39 @Override 40 public void onClick(View view) { 41 switch (view.getId()){ 42 case R.id.btnStartService: 43 startService(intent); 44 break; 45 case R.id.btnStopService: 46 stopService(intent); 47 System.out.println("服务已停止!!!"); 48 break; 49 case R.id.btnBindService: 50 bindService(intent,this,BIND_AUTO_CREATE); 51 break; 52 case R.id.btnUnBindService: 53 unbindService(this); 54 break; 55 } 56 } 57 58 @Override 59 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 60 System.out.println("服务已连接..."); 61 } 62 63 @Override 64 public void onServiceDisconnected(ComponentName componentName) { 65 System.out.println("服务已断开..."); 66 } 67 }
1 package com.example.metrox.l14; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 8 public class MyService extends Service { 9 10 private boolean isRuning = false; 11 public MyService() { 12 } 13 14 @Override 15 public IBinder onBind(Intent intent) { 16 return new Binder(); 17 } 18 19 @Override 20 public int onStartCommand(Intent intent, int flags, int startId) { 21 System.out.println("OnStartCommand"); 22 return super.onStartCommand(intent, flags, startId); 23 } 24 25 @Override 26 public void onCreate() { 27 super.onCreate(); 28 System.out.println("Service OnCreate"); 29 isRuning = true; 30 new Thread(){ 31 @Override 32 public void run() { 33 super.run(); 34 while (isRuning){ 35 System.out.println("服务正在运行中..."); 36 try { 37 sleep(1000); 38 } catch (InterruptedException e) { 39 e.printStackTrace(); 40 }} 41 } 42 }.start(); 43 } 44 45 @Override 46 public void onDestroy() { 47 super.onDestroy(); 48 System.out.println("Service Destroy"); 49 isRuning = false; 50 } 51 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.metrox.l14.MainActivity"> 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="Hello World!" 16 android:id="@+id/textView" /> 17 18 <Button 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="启动服务" 22 android:id="@+id/btnStartService" 23 android:layout_below="@+id/textView" 24 android:layout_alignParentLeft="true" 25 android:layout_alignParentStart="true" /> 26 27 <Button 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="停止服务" 31 android:id="@+id/btnStopService" 32 android:layout_below="@+id/btnStartService" 33 android:layout_alignParentLeft="true" 34 android:layout_alignParentStart="true" 35 android:layout_marginTop="54dp" /> 36 37 <Button 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:text="绑定服务" 41 android:id="@+id/btnBindService" 42 android:layout_centerVertical="true" 43 android:layout_alignParentLeft="true" 44 android:layout_alignParentStart="true" /> 45 46 <Button 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:text="解绑服务" 50 android:id="@+id/btnUnBindService" 51 android:layout_below="@+id/btnBindService" 52 android:layout_alignParentLeft="true" 53 android:layout_alignParentStart="true" 54 android:layout_marginTop="70dp" /> 55 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.metrox.l14"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity"> 12 <intent-filter> 13 <action android:name="android.intent.action.MAIN" /> 14 15 <category android:name="android.intent.category.LAUNCHER" /> 16 </intent-filter> 17 </activity> 18 19 <service 20 android:name=".MyService" 21 android:enabled="true" 22 android:exported="true"></service> 23 </application> 24 25 </manifest>
标签:
原文地址:http://www.cnblogs.com/linhongquan/p/5582515.html