各种通用方法

把资产目录里面的数据库写到SD卡上面

try {
            InputStream is = getResources().getAssets().open("antivirus.db");
            File file = new File(Environment.getExternalStorageDirectory(),
                    "antivirus.db");
            FileOutputStream fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.flush();
            fos.close();
            is.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

点击后退按钮,弹出对话框,然后杀死自己

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(MainActivity.this)
                    .setTitle("真的要离开?")
                    .setMessage("你确定要离开")
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener({
                                public void onClick(DialogInterface dialog,int which) {
                                    App app = (App) getApplication();
                                    AppManager.getAppManager().AppExit();
                                    //杀死自己
                                    android.os.Process.killProcess(android.os.Process.myPid());
                                }
                            }).show();

        }
        return true;
    }

子线程里面需要用到toast的标准用法

Looper.prepare();
Toast.makeText(getApplicationContext(), "备份完成", 1).show();
Looper.loop();

自定义toast

public static void  showMyToast(Context context, int icon , String text){
        Toast toast = new Toast(context);
        View view = View.inflate(context, R.layout.mytoast, null);
        TextView tv_toast = (TextView) view.findViewById(R.id.tv_toast);
        ImageView iv_toast = (ImageView) view.findViewById(R.id.iv_toast);
        tv_toast.setText(text);
        iv_toast.setImageResource(icon);

        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

监听短信SMS

  • 清单文件配置
<!--  配置一个短信的广播接收者,接收短信事件 -->
        <receiver android:name="com.xukunn.smsreceiver.SmsBroadcastReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/> //4.2开始不提供这个API了,不能Alt+/提示出来,只能手动去添加
            </intent-filter>
        </receiver>
  • 权限 android.permission.RECEIVE_SMS
  • 广播接收者
public class SmsBroadcastReceiver extends BroadcastReceiver {

    //pdu是ieee这个组织定义的短信的数据格式规范(一大堆的byte[],分别代表短信的类型,短信的接收时间,短信的发件人,服务短信号码) 有序广播。
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("发现新的,短信到来了。。");
        //取出来短信的数据
        Object[] objs = (Object[]) intent.getExtras().get("pdus");//短信数组。固定写法!!!!!!
        for(Object obj : objs){
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);//静态方法:把一组pdu类型的数据转成Message对象
            String body = smsMessage.getMessageBody();//得到短信内容
            String sender = smsMessage.getOriginatingAddress();//得到发件人地址
            System.out.println("body:"+body);
            System.out.println("sender:"+sender);
            if("5556".equals(sender)){
                abortBroadcast();
            }
        }
    }
}

把View对象挂载到窗体上面

public void showLoaction(String address) {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        params.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        params.setTitle("Toast");
        TextView view = new TextView(this);
        view.setText(address);
        WindowManager windowManager =  (WindowManager) this.getSystemService(WINDOW_SERVICE);
        windowManager.addView(view, params);
    }

自定义title

public class CustomtitleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 /*       //1.隐藏掉系统的标题栏
        //2.自定义的界面 上面显示出来的样式跟title类似 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);*/

        //第二种方式自定义title
        //告诉系统用自定义的title 
        boolean flag = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        if(flag){
            //设置系统的title为 自定义的那个资源文件 
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        }
    }
}

自定义dialog

 <style name="CustomDialog" parent="android:style/Theme.Dialog">

        <!-- 背景颜色及透明程度 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 是否有标题 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否模糊 -->
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowFrame">@null</item>
    </style>

自定义Notification

 public void click(View view){
        //1.创建notification 的管理器
        NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        //2 .创建notification的实例
        Notification notification = new Notification();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.icon = R.drawable.ic_launcher;
        notification.tickerText="自定义notification";
        //notification.contentView;
        //远程的view  我们的view对象 是要显示在另外一个进程里面 另外一个程序里面 所以就需要一个remote  view
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);
        rv.setTextViewText(R.id.textView1, "textview 自定义notification");
        rv.setProgressBar(R.id.progressBar1, 100, 50, false);

        notification.contentView = rv;
        Intent intent = new Intent(this,DemoActivity.class);
        PendingIntent  pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        notification.contentIntent = pendingIntent;

        manager.notify(2, notification);

    }

给图片加边框

/**
       * 添加图片边框
       * @param bmp
       * @return
       */
      public static Bitmap addFrame(Bitmap bmp,int borderColor,int borderSize)
      {
        Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize
            * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
        Canvas canvas = new Canvas(bmpWithBorder);
        canvas.drawColor(borderColor);//边框颜色
        canvas.drawBitmap(bmp, borderSize, borderSize, null);
        return bmpWithBorder;
      }

获取各种系统服务

获取位置的服务
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setCostAllowed(true); //设置是否允许产生费用
criteria.setSpeedRequired(true);//设置是否对速度敏感
criteria.setAltitudeRequired(true);//设置是否海拔敏感 
criteria.setAccuracy(Criteria.ACCURACY_FINE); //设置准确的定位 coarse 大体的位置
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = locationManager.getBestProvider(criteria, true);
//获取手机位置信息的变化 
locationManager.requestLocationUpdates(provider, 60000, 100, getListenerInstance());
获得超级管理员
DevicePolicyManager  manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
manager.wipeData(0);
//获取系统打电话的服务
TelephonyManager manager = (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);
//获取系统窗体服务
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

调节屏幕亮度

 public void setBrightness(int level) {
        ContentResolver cr = getContentResolver();
        Settings.System.putInt(cr, "screen_brightness", level);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        float flevel = level;
        attributes.screenBrightness = flevel / 255;
        getWindow().setAttributes(attributes);
    }

获取mac地址

1、<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    
2private String getLocalMacAddress() {  
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
    WifiInfo info = wifi.getConnectionInfo();  
    return info.getMacAddress();  
  }  

获取SD卡状态

/** 获取存储卡路径 */ 
File sdcardDir=Environment.getExternalStorageDirectory(); 
/** StatFs 看文件系统空间使用情况 */ 
StatFs statFs=new StatFs(sdcardDir.getPath()); 
/** Block 的 size*/ 
Long blockSize=statFs.getBlockSize(); 
/** 总 Block 数量 */ 
Long totalBlocks=statFs.getBlockCount(); 
/** 已使用的 Block 数量 */ 
Long availableBlocks=statFs.getAvailableBlocks(); 

禁用home键

先禁止Home键,再在onKeyDown里处理按键值,点击Home键的时候就把程序关闭,或者随你怎么处理。

@Override

 public boolean onKeyDown(int keyCode, KeyEvent event)

{ // TODO Auto-generated method stub

  if(KeyEvent.KEYCODE_HOME==keyCode)

    android.os.Process.killProcess(android.os.Process.myPid());

     return super.onKeyDown(keyCode, event);

  }

@Override

 public void onAttachedToWindow()

 { 
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();

 }

加权限禁止Home键

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>

开机启动

public class StartupReceiver extends BroadcastReceiver {  

  @Override  
  public void onReceive(Context context, Intent intent) {  
    Intent startupintent = new Intent(context,StrongTracks.class);  
    startupintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    context.startActivity(startupintent);  
  }  

}  
2)<receiver  
android:name=".StartupReceiver">  
<intent-filter>  
    <!-- 系统启动完成后会调用 -->  
    <action  
        android:name="android.intent.action.BOOT_COMPLETED">  
    </action>  
</intent-filter>  
</receiver> 

获取对话框的位置,挪动

 window =dialog.getWindow();//   
      WindowManager.LayoutParams wl = window.getAttributes();  
       wl.x = x;//这两句设置了对话框的位置.0为中间  
       wl.y =y;  
       wl.width =w;  
       wl.height =h;  
       wl.alpha =0.6f;// 这句设置了对话框的透明度 

移动dialog位置

//修改对话框的位置
Window mWindow = dialog.getWindow();  
WindowManager.LayoutParams lp = mWindow.getAttributes();  
lp.x = 10;   //新位置X坐标  
lp.y = -100; //新位置Y坐标  
dialog.onWindowAttributesChanged(lp); 

获取各种窗体高度

  //取得窗口属性
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        //窗口的宽度
        int screenWidth = dm.widthPixels;
        //窗口高度
        int screenHeight = dm.heightPixels;
二、获取状态栏高度
decorView是window中的最顶层view,可以从window中获取到decorView,
然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。 
于是,我们就可以算出状态栏的高度了。
view plain


Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;


三、获取标题栏高度
getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。
view plain


int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight

Android设置apn

ContentValues values = new ContentValues();
values.put(NAME, "CMCC cmwap");
values.put(APN, "cmwap");
values.put(PROXY, "10.0.0.172");

values.put(PORT, "80");
values.put(MMSPROXY, "");
values.put(MMSPORT, "");
values.put(USER, "");
values.put(SERVER, "");
values.put(PASSWORD, "");
values.put(MMSC, "");         
values.put(TYPE, "");
values.put(MCC, "460");
values.put(MNC, "00");
values.put(NUMERIC, "46000");
reURI = getContentResolver().insert(Uri.parse("content://telephony/carriers"), values);

//首选接入点"content://telephony/carriers/preferapn"

发送指令

out = process.getOutputStream();
out.write(("am start -a android.intent.action.VIEW -n com.android.browser/com.android.browser.BrowserActivity\n").getBytes());
out.flush();

InputStream in = process.getInputStream();
BufferedReader re = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = re.readLine()) != null) {
    Log.d("conio","[result]"+line);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值