Android中自定義權(quán)限android中為了安全性起見,對于應(yīng)用預(yù)定義了很多權(quán)限,,比如聯(lián)系人,,短信等等。而且其還支持自定義權(quán)限,,下面通過代碼示例,,表述一下自定義權(quán)限的定義以及使用。 首先定義一個Activity: package com.cust.perm; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; public class PrivilActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); LinearLayout layout=new LinearLayout(this); layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); layout.setOrientation(LinearLayout.VERTICAL); TextView txt=new TextView(this); txt.setText("hello from privilActivity"); layout.addView(txt); setContentView(layout); } } 而后在menifest.xml文件中進行權(quán)限的聲明: < activity android: android:permission="com.permission.my" > < intent-filter> < action android:/> < category android:/> < /intent-filter> < /activity> < /application> < uses-sdk android:minSdkVersion="4" /> < permission android:protectionLevel="normal" android:>< /permission> 請注意紅色部分,,核心配置,。 關(guān)于< permission>標簽中有很多屬性,,上述配置的這兩個屬性是必須的,,其他屬性含義如下: 接上頁 下面編寫另外一個項目,該項目中對于PrivilActivity進行使用,,假設(shè)該項目中有一個ClientActivity ,其功用是通過intent啟動PrivilActivity public class ClientActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.Button01); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClassName("com.cust.perm","com.cust.perm.PrivilActivity"); startActivity(intent); } }); } } 在其manifest.xml配置如下: < application android:icon="@drawable/icon" android:label="@string/app_name"> < activity android: android:label="@string/app_name"> < intent-filter> < action android: /> < category android: /> < /intent-filter> < /activity> < /application> < uses-sdk android:minSdkVersion="4" /> < uses-permission android:>< /uses-permission> 這樣就獲取了自定義權(quán)限,。 |
|