我应该如何在棉花糖中添加权限请求?
How should I add requests for the permission in marshmallow?
我正在尝试创建一次性密码访问 android 应用程序。我已经使用 wamp 创建了一个本地主机服务器,我正在尝试使用我设计的移动应用程序访问该服务器。不幸的是,我在日志 cat 中收到如下错误:
03-07 10:42:35.097 31217-31217/com.example.user.myapplication V/ActivityThread: updateVisibility : ActivityRecord{1ecab2fb token=android.os.BinderProxy@23c38b5b {com.example.user.myapplication/com.example.user.myapplication.activity.SmsActivity}} show : true
03-07 10:42:36.607 31217-31217/com.example.user.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@23c38b5b time:52633449
03-07 10:42:37.397 31217-31217/com.example.user.myapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
03-07 10:42:37.537 31217-32544/com.example.user.myapplication E/SmsActivity: Posting params: {email=thomasbi@gmail.com, name=Thomas, mobile=9002378900}
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
03-07 10:42:37.547 31217-31217/com.example.user.myapplication E/SmsActivity: Error: java.net.SocketException: socket failed: EACCES (Permission denied)
03-07 10:42:37.617 31217-31217/com.example.user.myapplication D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
我已经包含了所有权限,这是我的 AndroidManifest.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.user.myapplication">
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/MyMaterialTheme">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<activity
android:name=".activity.SmsActivity"
android:label="@string/title_activity_sms">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
</activity>
<!-- SMS Receiver -->
<receiver android:name=".receiver.SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- Intent service -->
<service
android:name=".service.HttpService"
android:exported="false" />
</application>
这是我的 permission.java 文件,是为在出现上述错误后提出请求而编写的:
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Permission extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Activity activity;
private static final int PERMISSION_REQUEST_CODE = 1;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.request_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = getApplicationContext();
activity = this;
Button check_permission = (Button)findViewById(R.id.check_permission);
Button request_permission = (Button)findViewById(R.id.request_permission);
check_permission.setOnClickListener(this);
request_permission.setOnClickListener(this);
}
@Override
public void onClick(View v) {
view = v;
int id = v.getId();
switch (id){
case R.id.check_permission:
if (checkPermission()) {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
}
break;
case R.id.request_permission:
if (!checkPermission()) {
requestPermission();
} else {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
}
break;
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.RECEIVE_SMS);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.RECEIVE_SMS)){
Toast.makeText(context,"SMS permission allows us to receive sms. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECEIVE_SMS},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar.make(view,"Permission Granted, Now you can access SMS.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Permission Denied, You cannot access SMS.",Snackbar.LENGTH_LONG).show();
}
break;
}
}
}
当我 运行 以下代码时,我在 log cat 中没有收到任何错误,并且应用程序停止并显示 PACKAGE INSTALLER HAS STOPPED..我现在有点困惑,我知道我被一些愚蠢的东西困住了。如果有人能帮助我解决这个问题,我会很高兴。
基本上你唯一需要申请的权限是短信权限(互联网不是危险的权限,你不必向用户申请)。 Google 建议您在使用之前先申请权限,而不是一次性申请所有权限(例如首次启动应用程序时)。 Google 还在他们的开发人员文档中提供了有关如何最好地实施以及何时实施的详细信息,这在此处很难总结。查看 developer documents here for code details, and the Material Design documents 如何根据您的应用的情况最好地实施。
如果您需要更多详细信息或关于权限的任何其他问题,请告诉我。
我正在尝试创建一次性密码访问 android 应用程序。我已经使用 wamp 创建了一个本地主机服务器,我正在尝试使用我设计的移动应用程序访问该服务器。不幸的是,我在日志 cat 中收到如下错误:
03-07 10:42:35.097 31217-31217/com.example.user.myapplication V/ActivityThread: updateVisibility : ActivityRecord{1ecab2fb token=android.os.BinderProxy@23c38b5b {com.example.user.myapplication/com.example.user.myapplication.activity.SmsActivity}} show : true
03-07 10:42:36.607 31217-31217/com.example.user.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@23c38b5b time:52633449
03-07 10:42:37.397 31217-31217/com.example.user.myapplication D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
03-07 10:42:37.537 31217-32544/com.example.user.myapplication E/SmsActivity: Posting params: {email=thomasbi@gmail.com, name=Thomas, mobile=9002378900}
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
03-07 10:42:37.537 31217-32544/com.example.user.myapplication I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
03-07 10:42:37.547 31217-31217/com.example.user.myapplication E/SmsActivity: Error: java.net.SocketException: socket failed: EACCES (Permission denied)
03-07 10:42:37.617 31217-31217/com.example.user.myapplication D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
我已经包含了所有权限,这是我的 AndroidManifest.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.user.myapplication">
<application
android:name=".app.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/MyMaterialTheme">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<activity
android:name=".activity.SmsActivity"
android:label="@string/title_activity_sms">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
</activity>
<!-- SMS Receiver -->
<receiver android:name=".receiver.SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<!-- Intent service -->
<service
android:name=".service.HttpService"
android:exported="false" />
</application>
这是我的 permission.java 文件,是为在出现上述错误后提出请求而编写的:
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Permission extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Activity activity;
private static final int PERMISSION_REQUEST_CODE = 1;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.request_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = getApplicationContext();
activity = this;
Button check_permission = (Button)findViewById(R.id.check_permission);
Button request_permission = (Button)findViewById(R.id.request_permission);
check_permission.setOnClickListener(this);
request_permission.setOnClickListener(this);
}
@Override
public void onClick(View v) {
view = v;
int id = v.getId();
switch (id){
case R.id.check_permission:
if (checkPermission()) {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
}
break;
case R.id.request_permission:
if (!checkPermission()) {
requestPermission();
} else {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
}
break;
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.RECEIVE_SMS);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.RECEIVE_SMS)){
Toast.makeText(context,"SMS permission allows us to receive sms. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.RECEIVE_SMS},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar.make(view,"Permission Granted, Now you can access SMS.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Permission Denied, You cannot access SMS.",Snackbar.LENGTH_LONG).show();
}
break;
}
} }
当我 运行 以下代码时,我在 log cat 中没有收到任何错误,并且应用程序停止并显示 PACKAGE INSTALLER HAS STOPPED..我现在有点困惑,我知道我被一些愚蠢的东西困住了。如果有人能帮助我解决这个问题,我会很高兴。
基本上你唯一需要申请的权限是短信权限(互联网不是危险的权限,你不必向用户申请)。 Google 建议您在使用之前先申请权限,而不是一次性申请所有权限(例如首次启动应用程序时)。 Google 还在他们的开发人员文档中提供了有关如何最好地实施以及何时实施的详细信息,这在此处很难总结。查看 developer documents here for code details, and the Material Design documents 如何根据您的应用的情况最好地实施。
如果您需要更多详细信息或关于权限的任何其他问题,请告诉我。