Android四大组件之Service
这个代码可以帮助不了解Service的朋友很直观的去学习Service;
AndroidManifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".Myservice"></service> </application> </manifest>
注意啊,在AndroidManifest.xml文件中要改成和你所创建的包名一致啊,(初学者)
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_marginTop="8dip" android:id="@+id/activitys_tv" android:layout_width="100dip" android:layout_height="30dip" android:text="Android四大组件Android四大组件" android:ellipsize="marquee" android:focusable="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:layout_marginLeft="105dip" android:focusableInTouchMode="true" android:scrollHorizontally="true" /> <TextView android:layout_marginTop="8dip" android:id="@+id/service_tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textview"/> <Button android:layout_marginTop="8dip" android:id="@+id/startservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startservice" /> <Button android:layout_marginTop="8dip" android:id="@+id/stopservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stopservice"/> <Button android:layout_marginTop="8dip" android:id="@+id/Bindservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bindservice" /> <Button android:layout_marginTop="8dip" android:id="@+id/Unbindservice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unbindservice"/> </LinearLayout>
Myservice.java代码:
import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class Myservice extends Service { MediaPlayer player ; public Myservice() { Log.i("Myservice", "构造方法---------->Myservice()"); } public class myIbind extends Binder{ } @Override public IBinder onBind(Intent arg0) { Log.i("Myservice", "onBind---------->"); if (player.isPlaying()) { player.stop(); }else { player.start(); } return new myIbind(); } @Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this,R.raw.danshen); Log.i("Myservice", "onCreate---------->"); } @Override public void onRebind(Intent intent) { Log.i("Myservice", "onRebind---------->"); super.onRebind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("Myservice", "onStartCommand---------->"); player = MediaPlayer.create(this,R.raw.danshen); player.start(); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.i("Myservice", "onUnbind---------->"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.i("Myservice", "onDestroy---------->"); player.stop(); super.onDestroy(); } }
MainActivity.java代码:
import com.example.android_service.Myservice.myIbind; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button startbtn,stopbtn,bindbtn,unbindbtn; private TextView tv ; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { //绑定意外断开时调用 Log.i("Myservice", "onServiceDisconnected》》》》》绑定失败"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { //绑定成功后调用 Log.i("Myservice", "onServiceConnected>>>>>>>>>绑定成功"); Myservice.myIbind mybind =(myIbind) service ; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startbtn = (Button) findViewById(R.id.startservice); stopbtn = (Button) findViewById(R.id.stopservice); bindbtn = (Button) findViewById(R.id.Bindservice); unbindbtn = (Button) findViewById(R.id.Unbindservice); tv = (TextView) findViewById(R.id.service_tv); startbtn.setOnClickListener(this); stopbtn.setOnClickListener(this); bindbtn.setOnClickListener(this); unbindbtn.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,Myservice.class); switch (v.getId()) { case R.id.startservice: startService(intent); break; case R.id.stopservice: stopService(intent); break; case R.id.Bindservice: bindService(intent, conn, BIND_AUTO_CREATE); break; case R.id.Unbindservice: unbindService(conn); break; default: break; } } }
注意:代码中R.raw.danshen中raw是在res文件夹下创建的新文件夹,danshen是一首歌的名字,将歌曲复制,粘贴到新建的raw文件夹中即可,此处是工程的关键点,当然,是对新手而言。
我的代码适合新手,所贴出的代码均是全部代码,复制到eclipse中即可使用,代码中基本没有注释,这是我和很多博友不同的地方,我先给初学者正确的代码,运行处真正的结果,然后去仔细看代码,既能增加兴趣,又有学习的节奏和目的性,所传代码,看似简单,但所学知识均匀涉及,当然,我也是新手,只是想借这个平台提升自己的境界,这很重要,好了,就到这里了。