酷狗音乐中的音乐播放
在Activity中实现
遇到的问题:
播放是没什么问题,但是退出界面后再次进去就没办法控制之前播放的音乐了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_main_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="play" />
<Button
android:id="@+id/btn_main_stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stop" />
<Button
android:id="@+id/btn_main_pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="pause" />
<Button
android:id="@+id/btn_main_exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="exit" />
</LinearLayout>
package com.jane.MusicService;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
private Button btn_main_play;
private Button btn_main_stop;
private Button btn_main_pause;
private Button btn_main_exit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_main_play = (Button) findViewById(R.id.btn_main_play);
btn_main_stop = (Button) findViewById(R.id.btn_main_stop);
btn_main_pause = (Button) findViewById(R.id.btn_main_pause);
btn_main_exit = (Button) findViewById(R.id.btn_main_exit);
btn_main_play.setOnClickListener(this);
btn_main_stop.setOnClickListener(this);
btn_main_pause.setOnClickListener(this);
btn_main_exit.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (btn_main_play == v)
{
// 播放
playMusic();
} else if (btn_main_stop == v)
{
// 停止播放
stopMusic();
} else if (btn_main_pause == v)
{
// 暂停音乐
pauseMusic();
} else if (btn_main_exit == v)
{
// 退出并停止音乐
exitMusic();
}
}
private MediaPlayer player;
private void exitMusic()
{
stopMusic();
finish();
}
/*
* 暂停音乐
*/
private void pauseMusic()
{
if (player != null && player.isPlaying())
{
player.pause();
}
}
private void stopMusic()
{
if (player != null)
{
player.stop();// 停止
player.reset();// 重置
player.release();// 释放资源
player = null;// 赋空
}
}
/**
* 播放音乐
*/
private void playMusic()
{
if (player == null)
{
player = MediaPlayer.create(this, R.raw.water_hander);
}
player.start();
}
}
在Service中实现
package com.jane.MusicService;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
private Button btn_main_play;
private Button btn_main_stop;
private Button btn_main_pause;
private Button btn_main_exit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_main_play = (Button) findViewById(R.id.btn_main_play);
btn_main_stop = (Button) findViewById(R.id.btn_main_stop);
btn_main_pause = (Button) findViewById(R.id.btn_main_pause);
btn_main_exit = (Button) findViewById(R.id.btn_main_exit);
btn_main_play.setOnClickListener(this);
btn_main_stop.setOnClickListener(this);
btn_main_pause.setOnClickListener(this);
btn_main_exit.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
Intent intent = new Intent(this, MusicService.class);
if (btn_main_play == v)
{
// 播放
intent.putExtra("action", "play");
startService(intent);
} else if (btn_main_stop == v)
{
// 停止播放
intent.putExtra("action", "stop");
startService(intent);
} else if (btn_main_pause == v)
{
// 暂停音乐
intent.putExtra("action", "pause");
startService(intent);
} else if (btn_main_exit == v)
{
// 退出并停止音乐
// 停止服务
stopService(intent);
finish();
}
}
}
package com.jane.MusicService;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
/**
* 播放音乐的Service
*/
public class MusicService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
private MediaPlayer player;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String action = intent.getStringExtra("action");
if ("play".equals(action))
{
playMusic();
} else if ("pause".equals(action))
{
pauseMusic();
} else if ("stop".equals(action))
{
stopMusic();
}
return super.onStartCommand(intent, flags, startId);
}
/*
* 暂停音乐
*/
private void pauseMusic()
{
if (player != null && player.isPlaying())
{
player.pause();
}
}
private void stopMusic()
{
if (player != null)
{
player.stop();// 停止
player.reset();// 重置
player.release();// 释放资源
player = null;// 赋空
}
}
/**
* 播放音乐
*/
private void playMusic()
{
if (player == null)
{
player = MediaPlayer.create(this, R.raw.water_hander);
}
player.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
// 在销毁之前停止音乐
stopMusic();
}
}
电话黑名单中自动挂断电话
Android没有对外公开结束通话的API,如果需要结束通话,
必须使用AIDL与电话管理服务进行通信,
并调用服务中的API实现结束通话,步骤是:
从sdk的源码中复制这个文件
com/android/internal/telephony/ITelephony.aidl
调用ITelephony.endCall()结束通话
public void endCall(View v) throws Exception
{
// 通过反射调用隐藏的API
// 得到隐藏类的Class对象
Class c = Class.forName("android.os.ServiceManager");
// 得到方法所对应的Method对象
Method method = c.getMethod("getService", String.class);
// 调用方法
IBinder iBinder = (IBinder) method.invoke(null,Context.TELEPHONY_SERVICE);
// 得到接口对象
ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
// 结束通话
telephony.endCall();
}
声明打/挂断电话的权限
相关的API
TelephonyManager: 电话服务的管理器
context.getSystemService(Context.TELEPHONY_SERVICE):得到它的对象
listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE):监听电话状态:
PhoneStateListener : 电话状态监听器
onCallStateChanged(int state, String incomingNumber): 电话状态改变的回调方法
电话的三种状态:
TelephonyManager.CALL_STATE_IDLE : 空闲状态
TelephonyManager.CALL_STATE_RINGING : 响铃状态
TelephonyManager.CALL_STATE_OFFHOOK : 接通状态
实现
package com.jane.callservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startListenCall(View v)
{
startService(new Intent(this, ListenCallService.class));
}
public void stopListenCall(View v)
{
stopService(new Intent(this, ListenCallService.class));
}
}
package com.jane.callservice;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ListenCallService extends Service
{
private TelephonyManager tm;
private PhoneStateListener listener = new PhoneStateListener()
{
/**
* Callback invoked when device call state changes.
*
* @see TelephonyManager#CALL_STATE_IDLE
* @see TelephonyManager#CALL_STATE_RINGING
* @see TelephonyManager#CALL_STATE_OFFHOOK
*/
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:// 空闲 (挂断电话/未来电之前)
Log.e("TAG", "空闲 (挂断电话/未来电之前)");
break;
case TelephonyManager.CALL_STATE_RINGING:// 响铃
Log.e("TAG", "响铃");
// 如果来电电话是黑名单号(110), 就挂断电话
if ("110".equals(incomingNumber))
{
try
{
endCall();
} catch (Exception e)
{
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:// 接通
Log.e("TAG", "接通");
break;
default:
break;
}
}
};
@Override
public IBinder onBind(Intent intent)
{
return null;
}
/**
* 挂断电话
* @throws Exception
*/
private void endCall() throws Exception
{
// 通过反射调用隐藏的API
// 得到隐藏类的Class对象
Class c = Class.forName("android.os.ServiceManager");
// 得到方法所对应的Method对象
Method method = c.getMethod("getService", String.class);
// 调用方法
IBinder iBinder = (IBinder) method.invoke(null,Context.TELEPHONY_SERVICE);
// 得到接口对象
ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
// 结束通话
telephony.endCall();
}
@Override
public void onCreate()
{
super.onCreate();
Log.e("TAG", "Service onCreate()");
// 得到电话管理器
tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
// 监听电话状态
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("TAG", "Service onDestroy()");
// 停止电话监听
tm.listen(listener, PhoneStateListener.LISTEN_NONE);
}
}