在来电的Andr​​oid收盘媒体服务 [英] Android closing media service on incoming call

查看:172
本文介绍了在来电的Andr​​oid收盘媒体服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很新的在Android和Java。我在做一个应用程序,它有一个媒体服务,我要被停止或暂停对来电的媒体。 这是我的媒体服务,code

I'm really new on android and java. I'm making an app and it has a media service and I want the media to be stopped or paused on incoming calls. This is my media service code

公共类ServiceMusic延伸服务{

public class ServiceMusic extends Service {

MediaPlayer music;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    music.stop();
    music.release();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    if (music !=null)
    music.stop();

    music = MediaPlayer.create(ServiceMusic.this, R.raw.music);
    music.start();
    music.setLooping(true);
}

}

我会真的AP preciate,如果你能帮助我,如果你能不能给我一个完整的指令,这将是巨大的。 谢谢

I will really appreciate if you can help me with this , and if you can give me a complete instruction that will be great. Thanks

推荐答案

您必须创建接收器,来电象下面这样:

you have to create receiver for incoming call like below:

public class call_reciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String number = "";
        Bundle bundle = intent.getExtras();


        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            // Phone is ringing
            number = bundle.getString("incoming_number");


        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // Call received

        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            // Call Dropped or rejected


        }


    }

在清单把这个:

<receiver android:name=".call_reciver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>

让你的媒体播放器变静态的放大器; globale,在reciver调用其他服务来检查您的媒体播放器正在运行。如果正在运行,然后停止。您可以使用也使用相同的服务。

make your media player variable static & globale, in reciver call another service to check your media player is running or not. if it is running then stop it. you can use also use same service.

您必须从接收器启动服务:

you have to start services from receiver:

Intent myIntent = new Intent(context, Service_temp.class);
        context.startService(myIntent);

在服务使用这样的:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
    }


查询编辑答案:

在下面的例子中,我已经做了所有的事情。有一个按钮,它会在服务打歌。这时候你在未来的呼叫,然后它会停止你的服务接收(停止播放的歌曲),当你拒绝或掉话然后重新开始播放歌曲

In below example I have done all things. there is one button it ll play song in service. at that time you receive in coming call then it will stop your service(stop playing song) and when you reject or drop call then it again start playing song

主要活动类:

package com.example.androidmediaplay;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void clickBtn(View v) {
        if (v.getId() == R.id.button1) {
            UtilClass.playing = true;
            Intent i = new Intent(MainActivity.this,
                    BackgroundSoundService.class);
            startService(i);
        }
    }

}

BackgroundSoundService类:

BackgroundSoundService class:

package com.example.androidmediaplay;

import java.io.File;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

public class BackgroundSoundService extends Service {

    private static final String TAG = null;
    MediaPlayer player;

    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(
                this,
                Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                        + "/Songs/test.mp3")));
        player.setLooping(true); // Set looping
        player.setVolume(100, 100);

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("onStartCommand", "onStartCommand");
        if (player.isPlaying()) {
            player.pause();
            UtilClass.pause = true;

            Log.e("onStartCommand pause", "onStartCommand pause");
        } else {
            UtilClass.pause = false;
            player.start();
        }

        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }

    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }

    public void onPause() {

    }

    @Override
    public void onDestroy() {
        UtilClass.playing = false;
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }

}

呼叫接收器类:

call receiver class:

package com.example.androidmediaplay;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;

public class call_reciver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String number = "";
        Bundle bundle = intent.getExtras();

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            // Phone is ringing
            number = bundle.getString("incoming_number");
            Log.e("incoming_number", "incoming_number");
            _stopServices(context);

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            // Call received

        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            // Call Dropped or rejected
            _stopServices(context);
        }

    }

    private void _stopServices(Context con) {
        // TODO Auto-generated method stub
        if (UtilClass.playing == true) {
            Log.e("start services", "start services");
            Intent i = new Intent(con, BackgroundSoundService.class);
            con.startService(i);
        } else {
            Log.e("start not services", "start not services");
        }
    }

}

额外类的静态成员:

extra class for static member:

package com.example.androidmediaplay;

public class UtilClass {

    public static Boolean playing = false;
    public static boolean pause = false;
}

在过去的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidmediaplay"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidmediaplay.MainActivity"
            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:name=".BackgroundSoundService"
            android:enabled="true" >
        </service>

        <receiver android:name=".call_reciver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

所有code是工作在我结束。请检查一下,然后告诉我。

All code is working at my end. please check it and tell me.

这篇关于在来电的Andr​​oid收盘媒体服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆