zoukankan      html  css  js  c++  java
  • Service2PlayService

    1.新建工程PlayService

    2.设置main.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7.       
    8.     <Button  
    9.     android:id="@+id/start"  
    10.     android:layout_width="fill_parent"  
    11.     android:layout_height="wrap_content"  
    12.     android:text="Start Play"     
    13.     />  
    14.       
    15.     <Button  
    16.     android:id="@+id/stop"  
    17.     android:layout_width="fill_parent"  
    18.     android:layout_height="wrap_content"  
    19.     android:text="Stop Play"     
    20.     />  
    21.       
    22. </LinearLayout>  

    3.新建Music.java

    1. package com.iceskysl.PlayService;  
    2.   
    3. import android.app.Service;  
    4. import android.content.Intent;  
    5. import android.media.MediaPlayer;  
    6. import android.os.IBinder;  
    7.   
    8. public class Music extends Service {  
    9.     private MediaPlayer player;  
    10.   
    11.     @Override  
    12.     public IBinder onBind(Intent intent) {  
    13.         // TODO Auto-generated method stub  
    14.         return null;  
    15.     }  
    16.       
    17.     public void onStart(Intent intent, int startId) {         
    18.         super.onStart(intent, startId);  
    19.         player = MediaPlayer.create(this, R.raw.gequ);  
    20.         player.start();  
    21.     }  
    22.      
    23.     public void onDestroy() {  
    24.         super.onDestroy();  
    25.         player.stop();  
    26.     }  
    27.   
    28. }  

    4.修改PlayService.java

    1. package com.iceskysl.PlayService;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Intent;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.view.View.OnClickListener;  
    8. import android.widget.Button;  
    9.   
    10. public class PlayService extends Activity {  
    11.     /** Called when the activity is first created. */  
    12.     @Override  
    13.     public void onCreate(Bundle savedInstanceState) {  
    14.         super.onCreate(savedInstanceState);  
    15.         setContentView(R.layout.main);  
    16.         Button button1 = (Button)findViewById(R.id.start);  
    17.         button1.setOnClickListener(startIt);  
    18.         Button button2 = (Button)findViewById(R.id.stop);  
    19.         button2.setOnClickListener(stopIt);  
    20.     }  
    21.       
    22.     private OnClickListener startIt = new OnClickListener()  
    23.     {  
    24.         public void onClick(View v)  
    25.         {             
    26.             startService(new Intent("com.iceskysl.PlayService.START_AUDIO_SERVICE"));  
    27.         }  
    28.     };  
    29.      
    30.     private OnClickListener stopIt = new OnClickListener()  
    31.     {  
    32.         public void onClick(View v)  
    33.         {  
    34.             stopService(new Intent("com.iceskysl.PlayService.START_AUDIO_SERVICE"));  
    35.             finish();             
    36.         }  
    37.     };  
    38. }  

    5.注册service

      <service android:name=".Music">
       <intent-filter>
        <action android:name="com.iceskysl.PlayService.START_AUDIO_SERVICE" />
        <category android:name="android.intent.category.default" />
       </intent-filter>
      </service>

    6.加入资源/res/raw/gequ.mp3

    当单击按钮启动播放Service后,在后台播放音乐,当执行其他操作时也不会打断音乐播放。

  • 相关阅读:
    hdu 5446 Unknown Treasure lucas和CRT
    Hdu 5444 Elven Postman dfs
    hdu 5443 The Water Problem 线段树
    hdu 5442 Favorite Donut 后缀数组
    hdu 5441 Travel 离线带权并查集
    hdu 5438 Ponds 拓扑排序
    hdu 5437 Alisha’s Party 优先队列
    HDU 5433 Xiao Ming climbing dp
    hdu 5432 Pyramid Split 二分
    Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造
  • 原文地址:https://www.cnblogs.com/lyz459/p/2541443.html
Copyright © 2011-2022 走看看