简体   繁体   English

调用需要 API 级别 23(当前最小值为 22)同时从应用程序发出调用请求

[英]Call requires API level 23 (current min is 22) While making a call request from App

I am doing an app for making a call directly from an image button inside my app.我正在做一个应用程序,用于直接从我的应用程序内的图像按钮拨打电话。 I want the app to be worked with both API 22 and 22. But while making the minSDK as 22, I am getting an error.我希望该应用程序同时使用 API 22 和 22。但是在将 minSDK 设为 22 时,我收到错误消息。

How to make it work??怎么让它工作??

MainActivity.java主活动.java

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    public Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this.mContext;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageButton:
                if (mContext.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + 4636));
                    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(callIntent);
                }
        break;
        }
    }

    }

The error is under checkSelfPermission错误在 checkSelfPermission 下

I had give the permission in Manifest...我已经在清单中给予了许可......

You have to asked the runtime permission while compiling agaisnt 6.0编译 6.0 runtime permission必须询问runtime permission

public static final int REQUEST_READ_PERMISSION =111;
// before on create as global variable 

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageButton:
               f (CheckPermission(this, Manifest.permission.CALL_PHONE)) {
                   Intent callIntent = new Intent(Intent.ACTION_CALL);
                   callIntent.setData(Uri.parse("tel:" + 4636));
                   callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   startActivity(callIntent);
                } else {
                RequestPermission(YourActivityName.this, Manifest.permission.CALL_PHONE, REQUEST_READ_PERMISSION );
               }
          break;
        }

 public boolean CheckPermission(Context context, String Permission) {
            if (ContextCompat.checkSelfPermission(context,
                    Permission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }

 public void RequestPermission(Activity thisActivity, String Permission, int Code) {
            if (ContextCompat.checkSelfPermission(thisActivity,
                    Permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        Permission)) {
                } else {
                    ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Permission},
                            Code);
                }
            }
        }

@Override
        public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {

            switch (permsRequestCode) {

                case REQUEST_READ_PERMISSION: {
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                         Intent callIntent = new Intent(Intent.ACTION_CALL);
                         callIntent.setData(Uri.parse("tel:" + 4636));
                         callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         startActivity(callIntent);
                    } else {
                        ShowToast(getString(R.string.permission_needed_sms));
                    }
                    return;
                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:调用需要API级别23(当前最小值为15): - Error : Call requires API level 23(current min is 15): 调用要求API级别21,当前最小值为8 - Call requires API level 21, current min is 8 调用需要API级别26(当前最小值为23):java.time.Instant#now - Call requires API level 26 (current min is 23): java.time.Instant#now 调用要求API级别16(当前最小值为14),并带有EndAction - Call requires API level 16 (current min is 14) withEndAction 调用需要API级别11(当前最小值为8)android.app.Activity#onCreateView - Call requires API level 11(current min is 8) android.app.Activity#onCreateView 调用需要API级别11(当前最小值为8):android.app.Activity#onCreateView FragmentActivity - Call requires API level 11 (current min is 8): android.app.Activity#onCreateView FragmentActivity “调用需要API级别23”错误,但是来自API 1的FrameLayout上存在getForeground() - 'Call requires API level 23' error, but getForeground() exists on FrameLayout from API 1 调用需要api级别9(当前最小值为7)java.text.normalizer#normalize - call requires api level 9 (current min is 7) java.text.normalizer#normalize 错误:调用需要 API 级别 24(当前最小值为 21):java.util.Map#forEach [NewApi] - Error: Call requires API level 24 (current min is 21): java.util.Map#forEach [NewApi] 如何修复 Android 中的“呼叫需要 API 级别 26(当前最小值为 25)”错误 - How to fix "Call requires API level 26 (current min is 25) " error in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM