在安卓系统中移除或隐藏通知栏

3

我正在开发一个应用程序,当应用程序打开时,如果有未接来电,则会显示通知,如何隐藏或删除通知栏,是否有实现的方法。 我已经在我的应用程序中放置了以下所有代码,

requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

并且

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

但是我使用广播接收器打开一个活动文件,当有未接来电或信息到达时,通知就会出现。
3个回答

3
您可以使用以下代码:
public class FullScreen
    extends android.app.Activity
{
    @Override
    public void onCreate(android.os.Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
    }
}

它没有工作,我正在询问应用程序是否已经在运行状态中,如果在此期间有任何未接来电,则会在上面出现通知栏,我已经设置了所有上述代码... - appukrb

1
我有和你一样的问题,但是我的问题发生在来电和去电时。我已经找到了来电/未接来电的解决方案,但对于去电还没有找到。
你需要做以下操作:

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

a.在Manifest.xml中添加:

    <receiver android:name=".MyPhoneBroadcastReceiver">
        <intent-filter android:priority="99999">
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

b.然后是类

@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);
        }

    }
}

2.a.在Manifest.xml文件中为您将用作自定义呼叫接收器的类添加此意图过滤器。

    <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>

2.b. 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);  }  }

3. 最后,当然不要忘记在Manifest.XML文件中添加主题以便不显示标题或通知栏。

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

我已经尝试了同样的技巧来处理呼出电话,但问题是当减少延迟(这是使所有这些发生的关键,我的屏幕显示并没有通知)时,我的呼叫屏幕不会出现,而是显示了Android的默认拨号器。如果你找到了解决方法,请分享。谢谢。 - KarimIhab
我也找到了来电的解决方案,但我没有出去电话的解决方案。不过我的想法是使外呼拨号具有紧急呼叫功能,这样就不允许用户返回主屏幕。如果你有任何关于如何制作紧急呼叫的想法,请告诉我。非常感谢您的回复... - appukrb
我尝试了一个非常不好的解决方案,但在找到更好的方法之前,我别无选择。问题是,在调用结束后,我为调用活动调用“this.finish”,然后调用一个活动,这是我的主屏幕,随后从第一个位置调用我的调用活动,这样我就可以刷新屏幕并使栏消失......就像我告诉你的那样,这是我想出的最糟糕的事情。如果您接受我的先前答案,请给我投票 :D - KarimIhab

0
你需要在 Android manifest .xml 中设置主题。
 android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 

希望这能对你有所帮助。
如果将其设置为应用程序主题,它将影响您的应用程序的所有页面。
   <application
         android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         >

对我来说它是有效的.. 我已经检查过了... 在编辑中查看代码以了解如何设置它。如果用户想要查看任何通知.. 他必须关闭或最小化您的应用程序。然后才能看到... - itsrajesh4uguys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接