MainActivity.Java

 1 package com.example.test; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 11 public class MainActivity extends Activity implements OnClickListener{12 13     private static final String TAG = "ServicesDemo";14       Button buttonStart, buttonStop;15 16       @Override17       public void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         setContentView(R.layout.activity_main);20 21         buttonStart = (Button) findViewById(R.id.buttonStart);22         buttonStop = (Button) findViewById(R.id.buttonStop);23 24         buttonStart.setOnClickListener(this);25         buttonStop.setOnClickListener(this);26       }27       28       public void onClick(View src) {29         switch (src.getId()) {30         case R.id.buttonStart:31           Log.d(TAG, "onClick: starting srvice");32           startService(new Intent(this, MyService.class));33           break;34         case R.id.buttonStop:35           Log.d(TAG, "onClick: stopping srvice");36           stopService(new Intent(this, MyService.class));37           break;38         }39       }40     }

MyService.Java

 1 package com.example.test; 2  3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.os.IBinder; 7 import android.util.Log; 8 import android.widget.Toast; 9 10 public class MyService extends Service {11         private static final String TAG = "MyService";12         MediaPlayer player;13        14         @Override15         public IBinder onBind(Intent intent) {16                 return null;17         }18        19         @Override20         public void onCreate() {21                 Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();22                 Log.d(TAG, "onCreate");23         }24 25         @Override26         public void onDestroy() {27                 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();28                 Log.d(TAG, "onDestroy");29         }30        31         @Override32         public void onStart(Intent intent, int startid) {33                 Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();34                 Log.d(TAG, "onStart");35         }36 }

activity_main.xml

 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     <Button12         android:id="@+id/buttonStart"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:layout_alignLeft="@+id/buttonStop"16         android:layout_alignParentTop="true"17         android:layout_marginTop="74dp"18         android:text="开始" />19 20     <Button21         android:id="@+id/buttonStop"22         android:layout_width="wrap_content"23         android:layout_height="wrap_content"24         android:layout_alignParentBottom="true"25         android:layout_centerHorizontal="true"26         android:layout_marginBottom="122dp"27         android:text="结束" />28 29 </RelativeLayout>

AndroidManifest.xml,添加

1 <service android:enabled="true" android:name=".MyService" />

更多相关文章

  1. NestedScrollView中添加一个android:fillViewport="true"
  2. 小区广播接收流程
  3. Android(安卓)添加桌面快捷方式操作
  4. Android之生成桌面快捷方式(二)
  5. Android之创建程序快捷方式
  6. android app 添加logo
  7. Retrofit简单使用(Kotlin)
  8. Android让帧动画在结束时消失
  9. Win7 64位 Android(安卓)SDK下载和更新失败的解决方法

随机推荐

  1. Android中如何修改系统时间
  2. Android(安卓)操作SQLite数据库(初步)-在程
  3. Android(安卓)Support Library
  4. Android(安卓)Studio主题设置
  5. Android(安卓)BackgroundViewPager:类似桌
  6. Android(安卓)RIL结构分析与移植
  7. Android开发包下载
  8. Android应用安全检测工具简介
  9. 《Android/OPhone 开发完全讲义》已出版,
  10. Android(安卓)APN设置