Skip to content

Commit

Permalink
huawei
Browse files Browse the repository at this point in the history
  • Loading branch information
mylhyl committed May 22, 2019
1 parent ea657f7 commit 1c28914
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 144 deletions.
22 changes: 15 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mylhyl.acp.sample">
package="com.mylhyl.acp.sample">

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.BODY_SENSORS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
android:allowBackup="true"
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".BlankActivity"></activity>
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/mylhyl/acp/sample/BlankActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.mylhyl.acp.sample;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand All @@ -23,6 +24,10 @@

public class BlankActivity extends AppCompatActivity {

public static void gotoAct(Activity act) {
act.startActivity(new Intent(act, BlankActivity.class));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -31,10 +36,6 @@ protected void onCreate(Bundle savedInstanceState) {
.add(android.R.id.content, BlankFragment.newInstance()).commitAllowingStateLoss();
}

public static void gotoAct(Activity act) {
act.startActivity(new Intent(act, BlankActivity.class));
}

public static class BlankFragment extends Fragment implements View.OnClickListener {

public BlankFragment() {
Expand All @@ -51,7 +52,8 @@ public static BlankFragment newInstance() {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
.LayoutParams.MATCH_PARENT));
textView.setGravity(Gravity.CENTER);
textView.setText(R.string.hello_blank_fragment);
textView.append("\n\nFragment中申请权限");
Expand All @@ -71,11 +73,14 @@ public void onGranted() {

@Override
public void onDenied(List<String> permissions) {
Toast.makeText(BlankFragment.this.getActivity(), permissions.toString() + "权限拒绝", Toast.LENGTH_SHORT).show();
Toast.makeText(BlankFragment.this.getActivity(), permissions.toString() + "权限拒绝", Toast
.LENGTH_SHORT).show();
}

});
}

@SuppressLint("MissingPermission")
private void getIMEI() {
FragmentActivity activity = this.getActivity();
TelephonyManager tm = (TelephonyManager) this.getActivity().getSystemService(Activity.TELEPHONY_SERVICE);
Expand Down
151 changes: 90 additions & 61 deletions app/src/main/java/com/mylhyl/acp/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.mylhyl.acp.sample;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

Expand All @@ -19,36 +21,32 @@
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
// static final int READ_PHONE_STATE_REQUEST_CODE = 0x38;
// static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 0x39;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.READ_PHONE_STATE
, Manifest.permission.SEND_SMS)
/*以下为自定义提示语、按钮文字
.setDeniedMessage()
.setDeniedCloseBtn()
.setDeniedSettingBtn()
.setRationalMessage()
.setRationalBtn()*/
.build(),
new AcpListener() {
@Override
public void onGranted() {
writeSD();
getIMEI();
}
public static File getCacheDir(String dirName, Context context) {
File result;
if (existsSdcard()) {
File cacheDir = context.getExternalCacheDir();
if (cacheDir == null) {
result = new File(Environment.getExternalStorageDirectory(),
"Android/data/" + context.getPackageName() + "/cache/" + dirName);
} else {
result = new File(cacheDir, dirName);
}
} else {
result = new File(context.getCacheDir(), dirName);
}
if (result.exists() || result.mkdirs()) {
return result;
} else {
return null;
}
}

@Override
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}
});
public static Boolean existsSdcard() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

public void onClickFragment(View v) {
Expand All @@ -60,12 +58,12 @@ public void onClickAll(View v) {
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.READ_PHONE_STATE
, Manifest.permission.SEND_SMS)
/*以下为自定义提示语、按钮文字
.setDeniedMessage()
.setDeniedCloseBtn()
.setDeniedSettingBtn()
.setRationalMessage()
.setRationalBtn()*/
/*以下为自定义提示语、按钮文字
.setDeniedMessage()
.setDeniedCloseBtn()
.setDeniedSettingBtn()
.setRationalMessage()
.setRationalBtn()*/
.build(),
new AcpListener() {
@Override
Expand All @@ -78,6 +76,7 @@ public void onGranted() {
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}

});
}

Expand All @@ -87,7 +86,8 @@ public void onClickSd(View v) {
// writeSD();
// } else {
// ActivityCompat.requestPermissions(this,
// new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
// new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
// WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
// }
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
Expand All @@ -102,6 +102,7 @@ public void onGranted() {
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}

});
}

Expand All @@ -112,13 +113,15 @@ public void onClickImei(View v) {
// getIMEI();
// } else {
// //处理“不再提醒”,判断是否需要 向用户解释,为什么要申请该权限
// if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) {
// if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
// .READ_PHONE_STATE)) {
// new AlertDialog.Builder(this).setMessage("需要申请imei")
// .setPositiveButton("申请", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// ActivityCompat.requestPermissions(MainActivity.this,
// new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE_REQUEST_CODE);
// new String[]{Manifest.permission.READ_PHONE_STATE},
// READ_PHONE_STATE_REQUEST_CODE);
// }
// }).create().show();
// } else {
Expand All @@ -140,6 +143,7 @@ public void onGranted() {
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}

});

}
Expand Down Expand Up @@ -168,13 +172,14 @@ public void onGranted() {
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}

});

}

//
// @Override
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
// grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// //用户选择允许或拒绝后,都会回调此方法
// switch (requestCode) {
Expand All @@ -201,42 +206,66 @@ public void onDenied(List<String> permissions) {
// return PackageManager.PERMISSION_GRANTED == permissionCheck;
// }

public void onClickCamera(View view) {
Acp.getInstance(this).request(new AcpOptions.Builder()
.setPermissions(Manifest.permission.CAMERA)
.build(),
new AcpListener() {
@Override
public void onGranted() {
makeText("同意");
}

@Override
public void onDenied(List<String> permissions) {
makeText(permissions.toString() + "权限拒绝");
}

});
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Acp.getInstance(this).request(new AcpOptions.Builder()
// .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE
// , Manifest.permission.READ_PHONE_STATE
// , Manifest.permission.SEND_SMS)
// /*以下为自定义提示语、按钮文字
// .setDeniedMessage()
// .setDeniedCloseBtn()
// .setDeniedSettingBtn()
// .setRationalMessage()
// .setRationalBtn()*/
// .build(),
// new AcpListener() {
// @Override
// public void onGranted() {
// writeSD();
// getIMEI();
// }
//
// @Override
// public void onDenied(List<String> permissions) {
// makeText(permissions.toString() + "权限拒绝");
// }
// });
}

private void writeSD() {
File acpDir = getCacheDir("acp", this);
if (acpDir != null)
makeText("写SD成功:" + acpDir.getAbsolutePath());
}

@SuppressLint("MissingPermission")
private void getIMEI() {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if (tm != null)
makeText("读imei成功:" + tm.getDeviceId());
}

public static File getCacheDir(String dirName, Context context) {
File result;
if (existsSdcard()) {
File cacheDir = context.getExternalCacheDir();
if (cacheDir == null) {
result = new File(Environment.getExternalStorageDirectory(),
"Android/data/" + context.getPackageName() + "/cache/" + dirName);
} else {
result = new File(cacheDir, dirName);
}
} else {
result = new File(context.getCacheDir(), dirName);
}
if (result.exists() || result.mkdirs()) {
return result;
} else {
return null;
}
}

public static Boolean existsSdcard() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

private void makeText(String text) {
Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
}
Expand Down
Loading

0 comments on commit 1c28914

Please sign in to comment.