【问题标题】:Hide notification bar when incoming call in android在android中来电时隐藏通知栏
【发布时间】:2013-08-20 13:05:01
【问题描述】:

当手机响铃时,我想运行一个活动来显示我自己的屏幕。

我正在使用:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

但显示活动时通知栏不会隐藏。

【问题讨论】:

    标签: android android-activity notifications android-notification-bar


    【解决方案1】:

    通过以下代码sn-ps:

    1. 创建一个 BroadCastReceiver 类来收听来电 最高优先级:

      Manifest.xml

      <receiver android:name=".MyPhoneBroadcastReceiver">
          <intent-filter android:priority="99999">
              <action android:name="android.intent.action.PHONE_STATE" />
          </intent-filter>
      </receiver>
      
    2. 然后在课堂上,onReceive() 方法打开CustomCallsReceiver Activity,意图动作是"android.intent.action.ANSWER"

      @Override
      public void onReceive(final Context context, Intent intent) {
      
      Bundle extras = intent.getExtras();
      
      if (extras != null) {
      
          String state = extras.getString(TelephonyManager.EXTRA_STATE);          
          final String incomingNumber = extras.getString("incoming_number");
      
          Handler callActionHandler = new Handler();
      
          Runnable runRingingActivity = new Runnable(){
              @Override
              public void run() {
                   //Notice the intent, cos u will add intent filter for your class(CustomCallsReceiver)
                  Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
                  intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
                  intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intentPhoneCall);
              }
          };
          if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
              //increase the delay amount if problem occur something like -the screen didn't show up- that's the key about this method(the delay).
              callActionHandler.postDelayed(runRingingActivity, 100);    
          }
          if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
              callActionHandler.removeCallbacks(runRingingActivity);
          }
      
      }
      }
      
    3. 将此意图过滤器添加到manifest file 中,用于您将要参加的课程 用作自定义呼叫接收器。

          <activity android:name="CustomCallsReceiver" android:noHistory="true" android:screenOrientation="portrait" >
          <intent-filter>
              <action android:name="android.intent.action.ANSWER" />
               <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>    
      </activity>
      
    4. CustomeCallsReceiver 类:

      public class CustomCallsReceiver extends Activity {
      
       private String TAG = "CustomCallsReceiver";
       String incomingNumber, caller;
      
       @Override
         public void onCreate(Bundle savedInstanceState) {
      
      super.onCreate(savedInstanceState);
      setContentView(R.layout.custome_calls_receiver);
      
      TextView number = (TextView) findViewById(R.id.number);
      number.setGravity(Gravity.CENTER);
      
      incomingNumber = getIntent().getExtras().getString("INCOMING_NUM");
      caller = getCallerName(incomingNumber);
      
      if (caller != null) {
          number.setText(caller + "\n" + incomingNumber);  }  
              }
      
    5. 最后当然不要忘记添加主题而不是标题或 manifest 文件中的通知栏

       <application
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
      

    希望这对你有用...

    【讨论】:

    • 坦克,但我创建这样的线程: public void run(){ Intent i = new Intent(); i.setClass(con, Call.class);...} 和活动显示正确,但通知栏多次显示在活动中(有时隐藏)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多