【移動應(yīng)用開發(fā)技術(shù)】Android 6.0中怎么申請動態(tài)權(quán)限_第1頁
【移動應(yīng)用開發(fā)技術(shù)】Android 6.0中怎么申請動態(tài)權(quán)限_第2頁
【移動應(yīng)用開發(fā)技術(shù)】Android 6.0中怎么申請動態(tài)權(quán)限_第3頁
【移動應(yīng)用開發(fā)技術(shù)】Android 6.0中怎么申請動態(tài)權(quán)限_第4頁
【移動應(yīng)用開發(fā)技術(shù)】Android 6.0中怎么申請動態(tài)權(quán)限_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】Android6.0中怎么申請動態(tài)權(quán)限

Android6.0中怎么申請動態(tài)權(quán)限?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。如果設(shè)備運行的是Android6.0(API級別23)或更高版本,并且應(yīng)用的targetSdkVersion是23或更高版本,則應(yīng)用在運行時向用戶請求權(quán)限。以下是需要單獨申請的權(quán)限,共分為9組,每組只要有一個權(quán)限申請成功了,就默認(rèn)整組權(quán)限都可以使用了group:android.permission-group.CONTACTS

permission:android.permission.WRITE_CONTACTS

permission:android.permission.GET_ACCOUNTS

permission:android.permission.READ_CONTACTS

group:android.permission-group.PHONE

permission:android.permission.READ_CALL_LOG

permission:android.permission.READ_PHONE_STATE

permission:android.permission.CALL_PHONE

permission:android.permission.WRITE_CALL_LOG

permission:android.permission.USE_SIP

permission:android.permission.PROCESS_OUTGOING_CALLS

permission:com.android.voicemail.permission.ADD_VOICEMAIL

group:android.permission-group.CALENDAR

permission:android.permission.READ_CALENDAR

permission:android.permission.WRITE_CALENDAR

group:android.permission-group.CAMERA

permission:android.permission.CAMERA

group:android.permission-group.SENSORS

permission:android.permission.BODY_SENSORS

group:android.permission-group.LOCATION

permission:android.permission.ACCESS_FINE_LOCATION

permission:android.permission.ACCESS_COARSE_LOCATION

group:android.permission-group.STORAGE

permission:android.permission.READ_EXTERNAL_STORAGE

permission:android.permission.WRITE_EXTERNAL_STORAGE

group:android.permission-group.MICROPHONE

permission:android.permission.RECORD_AUDIO

group:android.permission-group.SMS

permission:android.permission.READ_SMS

permission:android.permission.RECEIVE_WAP_PUSH

permission:android.permission.RECEIVE_MMS

permission:android.permission.RECEIVE_SMS

permission:android.permission.SEND_SMS

permission:android.permission.READ_CELL_BROADCASTS以下是普通權(quán)限,只需要在AndroidManifest.xml中申請即可

android.permission.ACCESS_LOCATION_EXTRA_COMMANDS

android.permission.ACCESS_NETWORK_STATE

android.permission.ACCESS_NOTIFICATION_POLICY

android.permission.ACCESS_WIFI_STATE

android.permission.ACCESS_WIMAX_STATE

android.permission.BLUETOOTH

android.permission.BLUETOOTH_ADMIN

android.permission.BROADCAST_STICKY

android.permission.CHANGE_NETWORK_STATE

android.permission.CHANGE_WIFI_MULTICAST_STATE

android.permission.CHANGE_WIFI_STATE

android.permission.CHANGE_WIMAX_STATE

android.permission.DISABLE_KEYGUARD

android.permission.EXPAND_STATUS_BAR

android.permission.FLASHLIGHT

android.permission.GET_ACCOUNTS

android.permission.GET_PACKAGE_SIZE

android.permission.INTERNET

android.permission.KILL_BACKGROUND_PROCESSES

android.permission.MODIFY_AUDIO_SETTINGS

android.permission.NFC

android.permission.READ_SYNC_SETTINGS

android.permission.READ_SYNC_STATS

android.permission.RECEIVE_BOOT_COMPLETED

android.permission.REORDER_TASKS

android.permission.REQUEST_INSTALL_PACKAGES

android.permission.SET_TIME_ZONE

android.permission.SET_WALLPAPER

android.permission.SET_WALLPAPER_HINTS

android.permission.SUBSCRIBED_FEEDS_READ

android.permission.TRANSMIT_IR

android.permission.USE_FINGERPRINT

android.permission.VIBRATE

android.permission.WAKE_LOCK

android.permission.WRITE_SYNC_SETTINGS

com.android.alarm.permission.SET_ALARM

com.android.launcher.permission.INSTALL_SHORTCUT

com.android.launcher.permission.UNINSTALL_SHORTCUT示例代碼/**

*

BaseActivity

*

*

@author

Edwin.Wu

*

@version

2017/3/17$

14:12$

*

@since

JDK1.8

*/

public

class

BaseActivity

extends

AppCompatActivity

{

@Override

protected

void

onCreate(@Nullable

Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

}

/**

*

為子類提供一個權(quán)限檢查方法

*

*

@param

permissions

*

@return

*/

public

boolean

hasPermission(String...

permissions)

{

for

(String

permission

:

permissions)

{

if

(ActivityCompat.checkSelfPermission(this,

permission)

!=

PackageManager.PERMISSION_GRANTED)

{

return

false;

}

}

return

true;

}

/**

*

權(quán)限請求申請

*

*

@param

requestCode

請求碼

*

@param

permissions

權(quán)限

*/

public

void

requestPermission(int

requestCode,

String...

permissions)

{

ActivityCompat.requestPermissions(this,

permissions,

requestCode);

}

@Override

public

void

onRequestPermissionsResult(int

requestCode,

@NonNull

String[]

permissions,

@NonNull

int[]

grantResults)

{

super.onRequestPermissionsResult(requestCode,

permissions,

grantResults);

switch

(requestCode)

{

case

PermissionsConstans.WRITE_STORAGE_CODE:

if

(grantResults[0]

==

PackageManager.PERMISSION_GRANTED)

{

doSDCardPermission();

}

else

{

//TODO

提示用戶權(quán)限未授予

Toast.makeText(BaseActivity.this,

"WRITE_EXTERNAL_STORAGE

權(quán)限未開啟",

Toast.LENGTH_SHORT).show();

}

break;

case

PermissionsConstans.CALL_PHONE_CODE:

if

(grantResults[0]

==

PackageManager.PERMISSION_GRANTED)

{

doCallPhone();

}

else

{

//TODO

提示用戶權(quán)限未授予

Toast.makeText(BaseActivity.this,

"ACTION_CALL

權(quán)限未開啟",

Toast.LENGTH_SHORT).show();

}

break;

default:

break;

}

}

/**

*

默認(rèn)的寫SD權(quán)限處理

*/

protected

void

doSDCardPermission()

{

//TODO

}

/**

*

默認(rèn)的打電話處理

*/

protected

void

doCallPhone()

{

//TODO

}

}/**

*

BaseActivity

*

*

@author

Edwin.Wu

*

@version

2017/3/17

11:59

*

@since

JDK1.8

*/

public

class

PermissionActivity

extends

BaseActivity

{

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_permission);

}

public

void

callPhone(View

view)

{

if

(hasPermission(android.Manifest.permission.CALL_PHONE))

{

doCallPhone();

}

else

{

requestPermission(PermissionsConstans.CALL_PHONE_CODE,

android.Manifest.permission.CALL_PHONE);

}

}

public

void

sdCardPermission(View

view)

{

if

(hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE))

{

doSDCardPermission();

}

else

{

requestPermission(PermissionsConstans.WRITE_STORAGE_CODE,

android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

}

}

@Override

protected

void

doCallPhone()

{

Intent

intent

=

new

Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:"

+

"10000"));

startActivity(intent);

}

@Override

protected

void

doSDCardPermission()

{

OkHttpClient

okHttpClient

=

new

OkHttpClient();

Request

request

=

new

Request

.Builder()

.url("/upload/information/20200623/125/132154.jpg")

.get()

.build();

Call

call

=

okHttpClient.newCall(request);

call.enqueue(new

Callback()

{

@Override

public

void

onFailure(Call

call,

IOException

e)

{

}

@Override

public

void

onResponse(Call

call,

Response

response)

throws

IOException

{

ResponseBody

body

=

response.body();

InputStream

is

=

body.byteStream();

int

len

=

0;

byte[]

bytes

=

new

byte[2048];

String

path

=

Environment.getExternalStorageDirectory().getAbsolutePath()

+

"/"

+

System.currentTimeMillis()

+

".jpg";

File

file

=

new

File(path);

FileOutputStream

fos

=

new

FileOutputStream(file);

while

((len

=

is.read(bytes))

!=

-1)

{

fos.write(bytes,

0,

len);

fos.flush();

}

Toast.makeText(PermissionActivity.this,

"SDCard寫入成功",

Toast.LENGTH_SHORT).show();

Log.e("Edwin",

"SDCard寫入成功");

}

});

}

}/**

*

PermissionGen框架

*

*

@author

Edwin.Wu

*

@version

2017/3/17

23:19

*

@since

JDK1.8

*/

public

class

PermissionGenActivity

extends

AppCompatActivity

{

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_permission_gen);

}

public

void

openCamera(View

view)

{

PermissionGen.needPermission(this,

200,

Manifest.permission.CAMERA);

}

public

void

openGroup(View

view)

{

PermissionGen

.with(this)

.addRequestCode(100)

.permissions(

Manifest.permission.READ_CONTACTS,

Manifest.permission.RECEIVE_SMS,

Manifest.permission.CAMERA,

Manifest.permission.WRITE_CONTACTS)

.request();

}

@PermissionSuccess(requestCode

=

100)

public

void

openGroupSuccess()

{

Toast.makeText(this,

"Group

Permission

Success",

Toast.LENGTH_SHORT).show();

startActivity(new

Intent(this,

ContactActivity.class));

}

@PermissionFail(requestCode

=

100)

private

void

openGroupFail()

{

Toast.makeText(this,

"Group

permission

is

not

granted",

Toast.LENGTH_SHORT).show();

}

@PermissionSuccess(requestCode

=

200)

public

void

openCameraSuccess()

{

Toast.makeText(this,

"Camera

permission

Success",

Toast.LENGTH_SHORT).show();

}

@PermissionFail(requestCode

=

200)

public

void

openCameraFail()

{

Toast.makeText(this,

"Camera

permission

is

not

granted",

Toast.LENGTH_SHORT).show();

}

@Override

public

void

onRequestPermissionsResult(int

requestCode,

String[]

permissions,

int[]

grantResults)

{

PermissionGen.onRequestPermissionsResult(this,

requestCode,

permissions,

grantResults);

}

}/**

*

RxPermissionGen框架

*

*

@author

Edwin.Wu

*

@version

2017/3/19

14:17

*

@since

JDK1.8

*/

public

class

RxPermissionActivity

extends

AppCompatActivity

{

private

static

final

String

TAG

=

"RxPermissionsSample";

private

Camera

camera;

private

SurfaceView

surfaceView;

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

RxPermissions

rxPermissions

=

new

RxPermissions(this);

rxPermissions.setLogging(true);

setContentView(R.layout.activity_rx_permission);

surfaceView

=

(SurfaceView)

findViewById(R.id.surfaceView);

RxView.clicks(findViewById(R.id.enableCamera))

//

Ask

for

permissions

when

button

is

clicked

.compose(rxPermissions.ensureEach(Manifest.permission.CAMERA))

.subscribe(new

Action1<Permission>()

{

@Override

public

void

call(Permission

permission)

{

Log.i(TAG,

"Permission

result

"

+

permission);

if

(permission.granted)

{

releaseCamera();

came

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論