日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Service的使用方法 音乐播放器实例

發布時間:2025/6/15 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Service的使用方法 音乐播放器实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Service翻譯成中文是服務,熟悉Windows 系統的同學一定知道很熟悉了。Android里的Service跟Windows里的Service功能差不多,就是一個不可見的進程在后臺執行,避免被用戶誤關閉。因為Android在某些情況下會自動關閉非前臺顯示的Activity,所以如果要讓一個功能在后臺一直執行,不被Android系統關閉,比如說鬧鐘、后臺播放音樂,就必須使用Service.
之前開發音樂播放器的時候也沒用Service,但是卻可以后臺播放,以為Service沒什么用,但是經過一段時間后發現,沒用Service的播放器在播放一段時間后會被系統自動關閉,而且就算還在后臺播放,過一段時間后打開播放器,再點播放按鈕,會出現兩種聲音,今天用Service寫了個Demo,完美解決以上問題。
布局XML,就兩個按鈕:

  • <?xml version="1.0" encoding="utf-8"?>
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  • android:orientation="vertical"
  • android:layout_width="fill_parent"
  • android:layout_height="fill_parent"
  • >
  • <Button android:id="@+id/start"
  • android:layout_width="fill_parent"
  • android:layout_height="wrap_content"
  • android:text="開始"
  • />
  • <Button android:id="@+id/stop"
  • android:layout_width="fill_parent"
  • android:layout_height="wrap_content"
  • android:text="停止"
  • />
  • </LinearLayout>
  • 主程序main.java:

  • package com.pocketdigi.service;
  • ?
  • ?
  • import android.app.Activity;
  • import android.content.Intent;
  • import android.os.Bundle;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.widget.Button;
  • ?
  • public class main extends Activity {
  • /** Called when the activity is first created. */
  • Button start,stop;
  • @Override
  • public void onCreate(Bundle savedInstanceState) {
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.main);
  • findView();
  • start.setOnClickListener(startlis);
  • stop.setOnClickListener(stoplis);
  • }
  • private OnClickListener startlis=new OnClickListener(){
  • ?
  • @Override
  • public void onClick(View v) {
  • // TODO Auto-generated method stub
  • startService(new Intent(main.this, Music.class));
  • //啟動服務
  • }
  • };
  • private OnClickListener stoplis=new OnClickListener(){
  • ?
  • @Override
  • public void onClick(View v) {
  • // TODO Auto-generated method stub
  • stopService(new Intent(main.this,Music.class));
  • //停止服務
  • }
  • };
  • public void findView(){
  • start=(Button)findViewById(R.id.start);
  • stop=(Button)findViewById(R.id.stop);
  • }
  • }
  • 服務 Music.java:

  • package com.pocketdigi.service;
  • ?
  • ?
  • import android.app.Service;
  • import android.content.Intent;
  • import android.media.MediaPlayer;
  • import android.os.IBinder;
  • ?
  • public class Music extends Service {
  • private MediaPlayer mp;
  • @Override
  • public IBinder onBind(Intent intent) {
  • // TODO Auto-generated method stub
  • return null;
  • }
  • @Override
  • public void onCreate() {
  • super.onCreate();
  • mp=MediaPlayer.create(this,R.raw.xrx);
  • }
  • @Override
  • public void onStart(Intent intent, int startId) {
  • super.onStart(intent, startId);
  • mp.start();
  • }
  • @Override
  • public void onDestroy() {
  • super.onDestroy();
  • mp.stop();
  • }
  • ?
  • }
  • 服務還需要在AndroidManifest.xml注冊后才能使用:

  • <?xml version="1.0" encoding="utf-8"?>
  • <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  • package="com.pocketdigi.service"
  • android:versionCode="1"
  • android:versionName="1.0">
  • <application android:icon="@drawable/icon" android:label="@string/app_name">
  • <activity android:name=".main"
  • 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:enabled="true" android:name=".Music" />
  • </application>
  • </manifest>
  • 總結

    以上是生活随笔為你收集整理的Android Service的使用方法 音乐播放器实例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。