コードに権限チェックを追加

4200 ワード

 1 import android.content.Context;  2 import android.content.pm.PackageManager;  3  4 public class Hello {  5  6 public static final String PERMISSION_SAY_HELLO = "com.example.authoritydemo.permission.SAY_HELLO";  7  8 public static void sayHello(Context cxt) {  9 int checkResult = cxt.checkCallingOrSelfPermission(PERMISSION_SAY_HELLO); 10 //   11 if (checkResult != PackageManager.PERMISSION_GRANTED) { 12 throw new SecurityException(" com.example.authoritydemo.permission.SAY_HELLO "); 13  } 14 System.out.println("Say Hello www.baidu.com"); 15  } 16 17 }
 1 public class MainActivity extends Activity {  2  3  @Override  4 protected void onCreate(Bundle savedInstanceState) {  5 super.onCreate(savedInstanceState);  6  setContentView(R.layout.activity_main);  7  8 Hello.sayHello(this);  9  } 10 11 }

プロファイルに追加
 <permission android:name="com.example.authoritydemo.permission.SAY_HELLO" /> <uses-permission android:name="com.example.authoritydemo.permission.SAY_HELLO" />