简体   繁体   English

Android Studio startActvity需要权限

[英]Android Studio startActvity requires permission

I'm attempting to make a simple click action which calls a certain number, I'm on the last stage of the code and I cannot see what I'm doing wrong. 我试图做一个简单的单击操作,调用一个特定的数字,我处于代码的最后阶段,看不到我在做什么错。 Currently its the startActivity action which seems to be presenting the error but I don't know why I have watched multiple tutorials and I can see any difference. 目前,它的startActivity动作似乎正在显示错误,但是我不知道为什么我看了多个教程,并且看到了任何区别。 When above startActivity it informs me that a call permission is required? 在startActivity之上时,它通知我需要通话许可?

 protected void onCreate(Bundle savedInstanceState) {


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
   getSupportActionBar().hide();
    setContentView(R.layout.activity_home);
    //On load the program automatically hides the taskbar.

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();


    Button b = (Button) this.findViewById(R.id.BTNCall);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v)
        {
            Intent PhoneCall = new Intent(Intent.ACTION_CALL);
            PhoneCall.setData(Uri.parse("tel:123"));
            startActivity(PhoneCall);
        }



    });


}

I have also added a permission into the android manifest 我还向android清单添加了权限

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

You should set your target SDK version to lvl 22 or under, because for lvl 23 you need to ask user at run time for permission, look there for a better explanation. 您应该将目标SDK版本设置为lvl 22或以下,因为对于lvl 23,您需要在运行时询问用户权限, 在此处查找更好的解释。

Since you are doing it from a OnClickListener, the "this" pointer references to the clickListener class you are implementing, you need to get the reference to your activity just use: 由于您是通过OnClickListener进行操作的,因此“ this”指针引用了您正在实现的clickListener类,因此您只需使用以下代码即可获取对您的活动的引用:

MyActivityClassName.this.startActivity() //Dont know your class name

That should get rid of the red highlight. 那应该摆脱红色突出显示。

If you are still getting a crash we will need the logcat output to solve it. 如果仍然崩溃,我们将需要logcat输出来解决它。

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM