2010-05-30 10 views
3

최근 안드로이드 프로그래밍 프로젝트에 문제가 발생했습니다. 문제는 전화를받을 때 시작되는 활동을 변경하고 싶습니다. 전화를받을 때 연락처 이름 뒤에 일부 텍스트를 추가 할 수 있습니까? 웹에서 검색 할 수있는 기능이 있으며 API를 몇 시간 동안보고 있었는데 아무 것도 찾을 수 없지만 그와 같은 것을 반사 할 수 있습니까? phone_state가 전화를 받았을 때 청취하는 클래스를 만들었습니다. 전화 번호를 얻을 수는 있지만 화면의 모양을 변경하고 싶습니다. 사전에 당신이 할 수있는Android : 전화를받을 때 활동 상태 변경

답변

1

, t 편집 InCallScreen 인터페이스

// 감사합니다. 이 토스트를 사용하여 위의하지만 당신은 broadcaste 수신기에서,

클래스 MyToast ............

package i4nc4mp.myLock.phone; 


/* 
* Copyright (C) 2007 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 



import android.app.INotificationManager; 
import android.app.ITransientNotification; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.PixelFormat; 
import android.os.Handler; 
import android.os.RemoteException; 
import android.os.ServiceManager; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.WindowManager; 
    import android.view.WindowManagerImpl; 
    import android.widget.TextView; 

/** 
* A toast is a view containing a quick little message for the user. The toast class 
* helps you create and show those. 
* {@more} 
* 
* <p> 
* When the view is shown to the user, appears as a floating view over the 
* application. It will never receive focus. The user will probably be in the 
* middle of typing something else. The idea is to be as unobtrusive as 
* possible, while still showing the user the information you want them to see. 
* Two examples are the volume control, and the brief message saying that your 
* settings have been saved. 
* <p> 
* The easiest way to use this class is to call one of the static methods that constructs 
* everything you need and returns a new Toast object. 
*/ 
public class MyToast { 
    static final String TAG = "Toast"; 
    static final boolean localLOGV = false; 

    /** 
    * Show the view or text notification for a short period of time. This time 
    * could be user-definable. This is the default. 
    * @see #setDuration 
    */ 
    public static final int LENGTH_SHORT = 0; 

    /** 
    * Show the view or text notification for a long period of time. This time 
    * could be user-definable. 
    * @see #setDuration 
    */ 
    public static final int LENGTH_LONG = 1; 

    final Handler mHandler = new Handler();  
    final Context mContext; 
    final TN mTN; 
    int mDuration; 
    int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; 
    int mX, mY; 
    float mHorizontalMargin; 
    float mVerticalMargin; 
    View mView; 
    View mNextView; 

    /** 
    * Construct an empty Toast object. You must call {@link #setView} before you 
    * can call {@link #show}. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} 
    *     or {@link android.app.Activity} object. 
    */ 
    public MyToast(Context context) { 
     mContext = context; 
     mTN = new TN(); 
     mY = context.getResources().getDimensionPixelSize(
       com.android.internal.R.dimen.toast_y_offset); 
    } 

    /** 
    * Show the view for the specified duration. 
    */ 
    public void show() { 
     if (mNextView == null) { 
      throw new RuntimeException("setView must have been called"); 
     } 

     INotificationManager service = getService(); 

     String pkg = mContext.getPackageName(); 

     TN tn = mTN; 

     try { 

      service.enqueueToast(pkg, tn, mDuration); 
     } catch (RemoteException e) { 
      // Empty 
     } 
    } 

    /** 
    * Close the view if it's showing, or don't show it if it isn't showing yet. 
    * You do not normally have to call this. Normally view will disappear on its own 
    * after the appropriate duration. 
    */ 
    public void cancel() { 
     mTN.myHide(); 
     // TODO this still needs to cancel the inflight notification if any 
    } 

    /** 
    * Set the view to show. 
    * @see #getView 
    */ 
    public void setView(View view) { 
     mNextView = view; 
    } 

    /** 
    * Return the view. 
    * @see #setView 
    */ 
    public View getView() { 
     return mNextView; 
    } 

    /** 
    * Set how long to show the view for. 
    * @see #LENGTH_SHORT 
    * @see #LENGTH_LONG 
    */ 
    public void setDuration(int duration) { 
     mDuration = duration; 
    } 

    /** 
    * Return the duration. 
    * @see #setDuration 
    */ 
    public int getDuration() { 
     return mDuration; 
    } 

    /** 
    * Set the margins of the view. 
    * 
    * @param horizontalMargin The horizontal margin, in percentage of the 
    *  container width, between the container's edges and the 
    *  notification 
    * @param verticalMargin The vertical margin, in percentage of the 
    *  container height, between the container's edges and the 
    *  notification 
    */ 
    public void setMargin(float horizontalMargin, float verticalMargin) { 
     mHorizontalMargin = horizontalMargin; 
     mVerticalMargin = verticalMargin; 
    } 

    /** 
    * Return the horizontal margin. 
    */ 
    public float getHorizontalMargin() { 
     return mHorizontalMargin; 
    } 

    /** 
    * Return the vertical margin. 
    */ 
    public float getVerticalMargin() { 
     return mVerticalMargin; 
    } 

    /** 
    * Set the location at which the notification should appear on the screen. 
    * @see android.view.Gravity 
    * @see #getGravity 
    */ 
    public void setGravity(int gravity, int xOffset, int yOffset) { 
     mGravity = gravity; 
     mX = xOffset; 
     mY = yOffset; 
    } 

    /** 
    * Get the location at which the notification should appear on the screen. 
    * @see android.view.Gravity 
    * @see #getGravity 
    */ 
    public int getGravity() { 
     return mGravity; 
    } 

    /** 
    * Return the X offset in pixels to apply to the gravity's location. 
    */ 
    public int getXOffset() { 
     return mX; 
    } 

    /** 
    * Return the Y offset in pixels to apply to the gravity's location. 
    */ 
    public int getYOffset() { 
     return mY; 
    } 

    /** 
    * Make a standard toast that just contains a text view. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} 
    *     or {@link android.app.Activity} object. 
    * @param text  The text to show. Can be formatted text. 
    * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or 
    *     {@link #LENGTH_LONG} 
    * 
    */ 
    public static MyToast makeText(Context context, CharSequence text, int duration) { 
     MyToast result = new MyToast(context); 

     LayoutInflater inflate = (LayoutInflater) 
       context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); 
     TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); 
     tv.setText(text); 

     result.mNextView = v; 
     result.mDuration = duration; 

     return result; 
    } 

    /** 
    * Make a standard toast that just contains a text view with the text from a resource. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} 
    *     or {@link android.app.Activity} object. 
    * @param resId The resource id of the string resource to use. Can be formatted text. 
    * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or 
    *     {@link #LENGTH_LONG} 
    * 
    * @throws Resources.NotFoundException if the resource can't be found. 
    */ 
    public static MyToast makeText(Context context, int resId, int duration) 
           throws Resources.NotFoundException { 
     return makeText(context, context.getResources().getText(resId), duration); 
    } 

    /** 
    * Update the text in a Toast that was previously created using one of the makeText() methods. 
    * @param resId The new text for the Toast. 
    */ 
    public void setText(int resId) { 
     setText(mContext.getText(resId)); 
    } 

    /** 
    * Update the text in a Toast that was previously created using one of the makeText() methods. 
    * @param s The new text for the Toast. 
    */ 
    public void setText(CharSequence s) { 
     if (mNextView == null) { 
      throw new RuntimeException("This Toast was not created with Toast.makeText()"); 
     } 
     TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message); 
     if (tv == null) { 
      throw new RuntimeException("This Toast was not created with Toast.makeText()"); 
     } 
     tv.setText(s); 
    } 

    // ======================================================================================= 
    // All the gunk below is the interaction with the Notification Service, which handles 
    // the proper ordering of these system-wide. 
    // ======================================================================================= 

    private static INotificationManager sService; 

    static private INotificationManager getService() { 
     if (sService != null) { 
      return sService; 
     } 
     sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification")); 
     return sService; 
    } 

    private class TN extends ITransientNotification.Stub { 
     final Runnable mShow = new Runnable() { 
      public void run() { 
       handleShow(); 
      } 
     }; 

     final Runnable mHide = new Runnable() { 
      public void run() { 
       handleHide(); 
      } 
     }; 

     private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(); 

     WindowManagerImpl mWM; 

     TN() { 
      // XXX This should be changed to use a Dialog, with a Theme.Toast 
      // defined that sets up the layout params appropriately. 
      final WindowManager.LayoutParams params = mParams; 
      params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
      params.width = WindowManager.LayoutParams.WRAP_CONTENT; 
      params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 
        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; 
      params.format = PixelFormat.TRANSLUCENT; 
      params.windowAnimations = com.android.internal.R.style.Theme_Dialog_Alert; 
      params.type = WindowManager.LayoutParams.TYPE_TOAST; 
      params.setTitle("Toast"); 
     } 

     /** 
     * schedule handleShow into the right thread 
     */ 
     public void show() { 
      if (localLOGV) Log.v(TAG, "SHOW: " + this); 
      mHandler.post(mShow); 
     } 

     /** 
     * schedule handleHide into the right thread 
     */ 
     public void hide() { 
      System.out.println("hide called"); 
      if (localLOGV) Log.v(TAG, "HIDE: " + this); 
      // mHandler.post(mHide); 
     } 
     public void myHide(){ 
      System.out.println("my hide called"); 
      if (localLOGV) Log.v(TAG, "HIDE: " + this); 
      mHandler.post(mHide); 
     } 
     public void handleShow() { 
      if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView 
        + " mNextView=" + mNextView); 
      if (mView != mNextView) { 
       // remove the old view if necessary 
       handleHide(); 
       mView = mNextView; 
       mWM = WindowManagerImpl.getDefault(); 
       final int gravity = mGravity; 
       mParams.gravity = gravity; 
       if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) { 
        mParams.horizontalWeight = 1.0f; 
       } 
       if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) { 
        mParams.verticalWeight = 1.0f; 
       } 
       mParams.x = mX; 
       mParams.y = mY; 
       mParams.verticalMargin = mVerticalMargin; 
       mParams.horizontalMargin = mHorizontalMargin; 
       if (mView.getParent() != null) { 
        if (localLOGV) Log.v(
          TAG, "REMOVE! " + mView + " in " + this); 
        mWM.removeView(mView); 
       } 
       if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this); 
       mWM.addView(mView, mParams); 
      } 
     } 

     public void handleHide() { 
      //System.out.println("handle hid ecalles"); 
      if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView); 
      if (mView != null) { 
       // note: checking parent() just to make sure the view has 
       // been added... i have seen cases where we get here when 
       // the view isn't yet added, so let's try not to crash. 
       if (mView.getParent() != null) { 
        if (localLOGV) Log.v(
          TAG, "REMOVE! " + mView + " in " + this); 
        mWM.removeView(mView); 
       } 

       mView = null; 
      } 
     } 
    } 
} 

// 일부 텍스트 또는 무엇이든을 표시 할 수 있습니다 ....... ...

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE) 

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
toast = new MyToast(context); 
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
      toast.setDuration(Toast.LENGTH_SHORT); 
toast.setView(view); 
toast.show(); 
    } 
else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
toast.cancel(); 
    } 
else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
toast.cancel(); 

} 
} 
+0

그렇다면 다음 링크는 어떻게 사용자 정의 수신 전화 화면을 보여줍니까? –

+0

링크를 아는 경우 질문에 답변하지 않는 이유는 무엇입니까? –

+0

링크가 누락되어 죄송합니다. http://mylifewithandroid.blogspot.com/2008/01/phonecalls.html –