Android的Service使用例子:酷狗音乐中的音乐播放,电话黑名单中自动挂断电话

酷狗音乐中的音乐播放

在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);
	}
}
世界地图矢量数据可以通过多种网站进行下载。以下是一些提供免费下载世界地图矢量数据的网站: 1. Open Street Map (https://www.openstreetmap.org/): 这个网站可以根据输入的经纬度或手动选定范围来导出目标区域的矢量图。导出的数据格式为osm格式,但只支持矩形范围的地图下载。 2. Geofabrik (http://download.geofabrik.de/): Geofabrik提供按洲际和国家快速下载全国范围的地图数据数据格式支持shape文件格式,包含多个独立图层,如道路、建筑、水域、交通、土地利用分类、自然景观等。数据每天更新一次。 3. bbbike (https://download.bbbike.org/osm/): bbbike提供全球主要的200多个城市的地图数据下载,也可以按照bbox进行下载。该网站还提供全球数据数据格式种类齐全,包括geojson、shp等。 4. GADM (https://gadm.org/index.html): GADM提供按国家或全球下载地图数据的服务。该网站提供多种格式的数据下载。 5. L7 AntV (https://l7.antv.antgroup.com/custom/tools/worldmap): L7 AntV是一个提供标准世界地图矢量数据免费下载的网站。支持多种数据格式下载,包括GeoJSON、KML、JSON、TopJSON、CSV和高清SVG格式等。可以下载中国省、市、县的矢量边界和世界各个国家的矢量边界数据。 以上这些网站都提供了世界地图矢量数据免费下载服务,你可以根据自己的需求选择合适的网站进行下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReflectMirroring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值