收藏学习,原文出处:http://blog.csdn.net/Android_Tutor/archive/2011/04/08/6310418.aspx#1638415
大家好,今天给大家简单分享一下Android中自定义action与permission. 我们在实际开发中,经常会用到系统的功能,比如打电话功能只要如以下简单代码:
view plaincopy to clipboardprint?
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
startActivity(it);
并且在AndroidManifest.xml文件中添加如下权限:
view plaincopy to clipboardprint?
<uses-permission id="android .permission.CALL_PHONE" />
<uses-permission id="android .permission.CALL_PHONE" />
下面我将实例给大家分享一下自定义action与permission.实例有两个Android工程Demo,与Demo2. Demo工程有两个Activity一个是主Activity,另一个是ViewActivity,这个Activity我们给自定义了android.tutor.action.VIEW的action以及com.tutor.permission.VIEW的permission。
另一个Demo2的主Activity去调用Demo里的ViewActivity,代码如下:
view plaincopy to clipboardprint?
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
并且要在AndroidMainfest.xml添加以下权限:
view plaincopy to clipboardprint?
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
下面是具体步骤如下:
第一步:新建第一个Android工程Demo.目录结构如下:
第二步:新建名为ViewActivity的Activity,代码如下:
view plaincopy to clipboardprint?
package com.tutor.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
mTextView.setText("我是自定义action并且加了权限的Activity.");
setContentView(mTextView);
}
}
package com.tutor.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
mTextView.setText("我是自定义action并且加了权限的Activity.");
setContentView(mTextView);
}
}
第三步:在AndroidMainfest.xml中定义action与permission,代码如下:
第6,7,20行代码定义了permission,第18-21行定义了action.第28行是同一个android工程访问ViewActivity也要申请权限.
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.demo"
android:versionCode="1"
android:versionName="1.0">
<permission android:protectionLevel="normal" android:name="com.tutor.permission.VIEW">
</permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Demo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewActivity"
android:label="自定义action与permission"
android:permission="com.tutor.permission.VIEW"
>
<intent-filter>
<action android:name="android.tutor.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.demo"
android:versionCode="1"
android:versionName="1.0">
<permission android:protectionLevel="normal" android:name="com.tutor.permission.VIEW">
</permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Demo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewActivity"
android:label="自定义action与permission"
android:permission="com.tutor.permission.VIEW"
>
<intent-filter>
<action android:name="android.tutor.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
</manifest>
第四步:修改主Activity,Demo.java代码如下(在onCreate()里去跳转到ViewActivity,也需要申请权限的如上面的第28行代码):
view plaincopy to clipboardprint?
package com.tutor.demo;
import com.tutor.demo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//跳转ViewActivity
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
}
}
package com.tutor.demo;
import com.tutor.demo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//跳转ViewActivity
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
}
}
第五步:运行Demo工程,效果如下图:
上面是同一个Android工程里访问ViewActivity的情形,下面我们新建一个Demo2的android工程,去调用ViewActivity.
Demo2.java代码如下:
view plaincopy to clipboardprint?
package com.tutor.demo2;
import com.tutor.demo2.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Demo2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//访问应用demo里的ViewActivity
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
}
}
package com.tutor.demo2;
import com.tutor.demo2.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Demo2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//访问应用demo里的ViewActivity
Intent mIntent = new Intent();
mIntent.setAction("android.tutor.action.VIEW");
startActivity(mIntent);
}
}
在Demo2工程里的AndroidMainifest.xml中申请权限,不加权限程序会报错,代码如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.demo2"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Demo2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.demo2"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Demo2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="com.tutor.permission.VIEW"></uses-permission>
</manifest>
运行之效果如下图所示:
一样的效果,呵呵,ok,今天就先讲到这里。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Android_Tutor/archive/2011/04/08/6310418.aspx#1638415