当前位置:操作系统 > 安卓/Android >>

Android 常用代码集合

[java] <span style="font-family: Arial, Verdana, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); ">这篇文章主要记录一些我常用的一些代码段,方便以后查阅,不断更新中</span> 
<span style="font-family: Arial, Verdana, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); ">这篇文章主要记录一些我常用的一些代码段,方便以后查阅,不断更新中</span>

 

1 调用浏览器 载入某网址
[java] Uri uri = Uri.parse("http://www.baidu.com");         
Intent it = new Intent(Intent.ACTION_VIEW, uri);         
startActivity(it); 
Uri uri = Uri.parse("http://www.baidu.com");       
Intent it = new Intent(Intent.ACTION_VIEW, uri);       
startActivity(it);

 

2 Broadcast接收系统广播的intent 监控应用程序包的安装 删除
[java] public class getBroadcast extends BroadcastReceiver { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
                
                  if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show(); 
            } 
                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show(); 
            } 
              
                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){ 
                    Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show(); 
            } 
                   
                else  if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){ 
                    Toast.makeText(context, "按键", Toast.LENGTH_LONG).show(); 
            } 
             
        } 
        

public class getBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
              
                  if(Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被添加", Toast.LENGTH_LONG).show();
            }
                else  if(Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被删除", Toast.LENGTH_LONG).show();
            }
            
                else  if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
                    Toast.makeText(context, "有应用被替换", Toast.LENGTH_LONG).show();
            }
                 
                else  if(Intent.ACTION_CAMERA_BUTTON.equals(intent.getAction())){
                    Toast.makeText(context, "按键", Toast.LENGTH_LONG).show();
            }
           
        }
      
}

需要声明的权限如下AndroidManifest.xml

[c-sharp] <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="zy.Broadcast" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".Broadcast" 
                  android:label="@string/app_name"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
      <receiver android:name="getBroadcast" android:enabled="true" > 
         <intent-filter> 
             <action android:name="android.intent.action.PACKAGE_ADDED"></action> 
 &nb

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,