前言:
前一篇在這裡:http://nikeru8.blogspot.tw/2017/04/android-permission.html
這篇只是把Permission和Intent做個封裝。
大致上長這樣。
讓我們開始吧。
重點程式碼:
//設定Activity頁面參數 private Class<?> mClss; public void launchActivity(Class<?> clss) { //如果沒有權限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { mClss = clss; ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CUSTOM_NUMBER); } else { Intent intent = new Intent(this, clss); startActivity(intent); } }
一言蔽之,就是如果你要使用相機功能,沒有權限的話幫你導到權限畫面,如果之前已經取過權限,則直接進入。
實作:
先來看看我們今天會動到的地方。先創一個Activity,我這邊命名為 activity_part_two.xml和PartTwoActivity
先給予這App權限吧。
STEP 1:給予相機權限
AndroidManifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spinner.a015865.permission"> <uses-permission android:name="android.permission.CAMERA"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".PartTwoActivity"/> </application> </manifest>
STEP 2:創建第二個Activity並畫XML
activity_main.xml |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.spinner.a015865.permission.MainActivity"> <Button android:text="要權限+切到畫面B" android:onClick="Button_to_next" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
activity_part_two.xml |
activity_part_two.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.spinner.a015865.permission.PartTwoActivity"> <TextView android:textSize="50dp" android:text="BBBBBBBBB" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
STEP 3:實作JAVA部分
第一個Activity才是重點。第二個新創的Activity是空的,示意一下而已,在這邊把所有的程式碼都貼上以示完整。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Class<?> mClss;
private static final int CUSTOM_NUMBER = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Button_to_next(View view) {
launchActivity(PartTwoActivity.class);
}
public void launchActivity(Class<?> clss) {
//如果沒有權限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
mClss = clss;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
CUSTOM_NUMBER);
} else {
Intent intent = new Intent(this, clss);
startActivity(intent);
}
}
}
PartTwoActivity
public class PartTwoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_part_two); } }
完成囉!
Demo:
https://drive.google.com/open?id=0Byk75IYx-dKXeV9nOWFWOHFIc1E
沒有留言:
張貼留言