Android-禁用返回和主页按钮

时间:2018-12-21 10:06:40

标签: android

如何在Android应用程序中禁用“返回”和“主页”按钮。这样我的应用程序将不会通过单击后退或主页按钮关闭。

2 个答案:

答案 0 :(得分:1)

覆盖后退方法,将其留空。

创建/** Make just the non-observable part observable. Then we can simply add it to the chain */ getLatLon(address): Observable<{lat: any, lon: any}> { return Observable.create(observer => { service.geocode({address: params.address}, results => { // avoid side effects extra information. const lat = results[0].geometry.location.lat(); const lon = results[0].geometry.location.lng(); observer.next({ lat, lon }); observer.complete(); }); } // Also // Now you can use it in a regular chain. search(params): Observable<Calendar[]> { return this.getLatLon(params) .pipe( map(coords => { // Again, we will avoid side effects. const fullParams = Object.assign({}, params, coords) return fullParams; }), map(fullParams => this.apiService.get(`/api/v1/website/${ environment.web_id }/calendar/_search`, params)), ); } 时,只需删除onBackPressed()即可,

super.onBackPressed();

答案 1 :(得分:0)

默认情况下,返回键和主键点击事件在android框架中处理。如果要更改这两个软按钮的行为,则需要在应用程序活动中进行处理。
下面的代码段显示了一个简单的代码,即使在向后或按下主功能键时,也可以将当前活动保持在前面。

public class MyActivity extends AppCompatActivity {

// .. Other codes ...
//....
    @Override
    protected void onPause() {

       super.onPause();
       ActivityManager activityManager = (ActivityManager) getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);
       activityManager.moveTaskToFront(getTaskId(), 0);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       return true;
    }

    @Override
    public void onBackPressed() {}

}

请参见此处的替代。还要在AndroidManifest.xml中添加REORDER_TASKS权限

AndroidManifest.xml

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