Android之Service
实例:ServiceDemo
运行效果:
代码清单:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rainsong.servicedemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="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" /> </application> </manifest>
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service" /> <Button android:id="@+id/btn_stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service" /> </LinearLayout>
Java源代码文件:MainActivity.java
package com.rainsong.servicedemo; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn_start; Button btn_stop; OnClickListener listener_start; OnClickListener listener_stop; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listener_start = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, MyService.class); startService(intent); } }; listener_stop = new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainActivity.this, MyService.class); stopService(intent); } }; btn_start = (Button)findViewById(R.id.btn_start); btn_start.setOnClickListener(listener_start); btn_stop = (Button)findViewById(R.id.btn_stop); btn_stop.setOnClickListener(listener_stop); } }
Java源代码文件:MyService.java
package com.rainsong.servicedemo; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; import android.util.Log; public class MyService extends Service { @Override public IBinder onBind(Intent i) { return null; } @Override public boolean onUnbind(Intent i) { return false; } @Override public void onCreate() { Toast.makeText(this, "Service onCreate", Toast.LENGTH_SHORT).show(); } @Override public void onStart(Intent i, int startId) { Toast.makeText(this, "Service onStart", Toast.LENGTH_SHORT).show(); } @Override public void onDestroy() { Toast.makeText(this, "Service onDestroy", Toast.LENGTH_SHORT).show(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。