Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Orfen · Mar 16, 2015 at 01:09 PM · androidcallpopupmute

Muting application during phonecall on Android(Call Popup)

Apparently there has been a new thing i wasn't aware of, and that is call popups. What this essentially does is it prompts you to answer/mute/decline the call without never leaving the current app you are focused on. Of course, since the app doesn't lose focus Unity doesn't mute it. Does anyone have a possible workaround for this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Nonatomic · Mar 26, 2015 at 03:10 PM

Experienced the same issue. There's a setting on device under Settings -> Call-> Call-related pop-ups called Call notification pop-ups which can be toggled to test this.

I wrote a native extension to get the phone call status of the device. This can be extended to mute your application during a call.


Java code

 package com.your.app.package;
 
 import android.content.Context;
 import android.os.Bundle;
 import android.telephony.PhoneStateListener;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 import com.unity3d.player.UnityPlayerActivity;
 import com.unity3d.player.UnityPlayer;
 
 public class CallStatusBridge extends UnityPlayerActivity
 {    
     private static int currentState = 0;
     private static String gameObject = "callstate";
     private static String callBackName = "OnCallStateChange";
     
     protected void onCreate(Bundle savedInstanceState) 
     {
         Log.d("Unity", "CallStatus created");
         
         super.onCreate(savedInstanceState);        
         TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
         
         PhoneStateListener phoneStateListener = new PhoneStateListener()
         {    
             @Override
             public void onCallStateChanged(int state, String incomingNumber)
             {    
                 switch (state)
                 {
                     case TelephonyManager.CALL_STATE_IDLE:
                         currentState = 0;
                         break;
                     case TelephonyManager.CALL_STATE_RINGING:
                         currentState = 1;
                         break;
                     case TelephonyManager.CALL_STATE_OFFHOOK:  
                         currentState = 2;
                         break;
                     default:
                         currentState = 0;
                         break;
                 }
                 
                 Log.d("Unity", "CurrentState:" + currentState);
                 sendCallStateToUnity(currentState);
             }
         };
         telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
      }
 
     public static int getCallStatus()
     {
         return currentState;
     }
     
     public static void setCallBack(String gameObject, String callBackName)
     {
         Log.d("Unity", "setCallBack");
         CallStatusBridge.gameObject = gameObject;
         CallStatusBridge.callBackName = callBackName;
     }
 
     public static void sendCallStateToUnity(int state){
         Log.d("Unity", "sendCallState");
         UnityPlayer.UnitySendMessage(gameObject, callBackName, "" + state);
     }
 }



C# code

 using UnityEngine;
 using System.Collections;
 
 public class CallStateBridge : MonoBehaviour {
 
     #if UNITY_ANDROID
         AndroidJavaObject jc;
     #endif
 
     void Start ()
     {
         #if UNITY_ANDROID
             AndroidJNI.AttachCurrentThread();
             jc = new AndroidJavaClass("com.ea.android.theimpossibleline.rowsamsung.CallStatusBridge");
             jc.CallStatic("setCallBack", new object[2] {gameObject.name, "OnCallStateChange"});
         #endif
     }
 
     public void OnCallStateChange(string state){
         Debug.Log("call status:" + state);
     }
 
     #if UNITY_ANDROID
         public int CheckCallStatus(){
             return jc.CallStatic<int>("getCallStatus");
         }
     #endif
 }


Add this to your Android Manifest in Unity

 <!-- CallStatusBridge -->
 <activity android:name=".CallStatusBridge"
           android:label="@string/app_name">
     <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
 </activity>
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>









Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yury-Habets ♦♦ · Mar 15, 2017 at 03:41 PM 0
Share

This is a bad piece of code because it requires READ_PHONE_STATE permission. It's already implemented withing Unity, but in a less intrusive way.

avatar image
0

Answer by Orfen · Sep 25, 2015 at 07:56 AM

I ended up implementing a very similar solution to yours, but not having a bridge, just extending the unity activity and doing everything on the native side.

 public class UnityPlayerNativeActivity extends NativeActivity
 {
     protected UnityPlayer mUnityPlayer;  // don't change the name of this variable; referenced from native code
     private Boolean wasPlaying;
     // Setup activity layout
     @Override protected void onCreate (Bundle savedInstanceState)
     {
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         super.onCreate(savedInstanceState);
 
         getWindow().takeSurface(null);
         getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
 
         mUnityPlayer = new UnityPlayer(this);
         if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
             getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
 
         setContentView(mUnityPlayer);
         mUnityPlayer.requestFocus();
         wasPlaying=true;
         TelephonyManager mgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
         mgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
     }
     PhoneStateListener mPhoneStateListener = new PhoneStateListener()
     {
 
         @Override
         public void onCallStateChanged(int state, String incomingNumber)
         {
             AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 
             if( state == TelephonyManager.CALL_STATE_RINGING )
             {
                 if(wasPlaying){
                     am.setStreamMute(AudioManager.STREAM_MUSIC, true);
                     wasPlaying=false;
                 }
             }
             else if(state == TelephonyManager.CALL_STATE_IDLE )
             {  // Not in call: Play music
                 if(wasPlaying==false){
                     am.setStreamMute(AudioManager.STREAM_MUSIC, false);
                     wasPlaying=true;
                 }
 
             }
             else if( state == TelephonyManager.CALL_STATE_OFFHOOK )
             {
                if(wasPlaying==true){
                     am.setStreamMute(AudioManager.STREAM_MUSIC, true);
                     wasPlaying=false;
                 }
             }
 
             super.onCallStateChanged(state, incomingNumber);
         }
     };

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by sandeepsmartest · Sep 25, 2015 at 12:40 PM

Try using "OnApplicationPause(bool PauseState)" based on the PauseStateboolean value try decreasing/increasing or switching off audio listener or making audio source mute. Hope this may help you.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keyboard doesn't popup on Android 4.4 with Unity 3.5 1 Answer

Disable/Enable Bluetooth on Android (NATIVE) 1 Answer

Read intent from Android Studio in Unity 0 Answers

popup message with a button 0 Answers

Android native popup window 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges