android经验总结

短信的拦截操作

注册一个短信的广播接受者

onRceive()

通过Intent 获取短信的数据 

Bundle bundle

Bundle bundle = new Bundle();

bundle.putSerializable("puds", new Object (需要实现serializalbe));

Object pdus[] = (Object[]) intent.getExtras().get("pdus");

       for (Object pdu : pdus) {

//通过puds的数据构建出短信

           SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);

//获取短信的具体内容

String body= messge.getMessageBody();

//获取短信的发送者

           String sender = message.getOriginatingAddress();

}

//拦截短信

abortBroadcast();


数据库的操作 ()

自定义一个 class  DBOpenHelper extends SQLiteOpenHelper

1. 生成 DBOponeHelper 的构造方法

   4个参数 上下文 ,数据库名,游标工厂, 数据库版本号

(context, "blacknumber.db", null, 1);

  1. 2.  重写 onCreate 方法 创建数据库的表.  Db.execSQL(); 

 约定 根据sqlite的标准 最好把组件的名字设置为_id

 CursorAdpater  "_id" notfound exception

3. onUpgrade 帮助更新数据库的表

(BlackNumberDao.java)

需要定义个类 DBdao  (data access object)

1.构造方法 把context 传递 进来

 

2.根据业务逻辑去实现增删改查的方法 

 

public BlackNumberDao(Context context) {

       dbOpenHelper = new DBOpenHelper(context);

    }

   

    /*

     * 添加 黑名单号码

     * params number : 要添加的黑名单号码

     */

    public void save(String number){

Insert xxx xxx ...

    }

    /*

     * 查询的黑名单号码

     */

    public boolean query(String number){

 

    }

    /*

     * 删除黑名单号码

     */

    public void delete (String number){

 

    }

   

    /*

     * 查询所有的记录

     */

    public List<String> findAll(){

 

    }


监听电话状态  (ShowAddressService.java)

TelephonyManager telephonyManager;

 

初始化

       telephonyManager = (TelephonyManager) getApplicationContext()

              .getSystemService(Context.TELEPHONY_SERVICE);

 

设置telephonyManager 监听的 linstner

 

创建一个 class  MyPhoneLinstener extends PhoneStateListener

重写 onCallStateChanged(

public void onCallStateChanged(int state, String incomingNumber) {

State 电话的状态: 铃声正在响  挂断  ,空闲

incomingNumber 来电号码

 

 

 

挂断电话: (ShowAddressService.java)

Android 1.5之前  telephony.endCall();

Aidl 调用 系统远程服务里面的方法

需要用到两个aidl文件  可以去android 的 telephony.apk的代码获取

       Method method = Class.forName("android.os.ServiceManager")

              .getMethod("getService", String.class);

       IBinder binder = (IBinder) method.invoke(null,

              new Object[] { Context.TELEPHONY_SERVICE });

 

 

通过content Resolver 删除呼叫记录(ShowAddressService.java)

    public void deleteCallLog(String incomingNumber ) {

       // TODO Auto-generated method stub

       ContentResolver resolver = getApplicationContext().getContentResolver();

       Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI,

              new String[]{"_id"},

              "number=?",

              new String[]{incomingNumber},

              null);

       //如果获取通话的结果

       if(cursor.moveToFirst()){

           resolver.delete(CallLog.Calls.CONTENT_URI, "_id="+cursor.getInt(0), null);

        }

    }

 

内容观察者

通过 contentresolver注册一个内容观察者

resolver.registerContentObserver(CallLog.Calls.CONTENT_URI,

                            truenew CallLogChangeListener(new Handler(),incomingNumber));

 

自定义一个contentObserver   

public class CallLogChangeListener extends ContentObserver{

 

 

}

重写 CallLogChangeListener 方法

当呼叫日志发生改变的时候  去删除呼叫记录

 

 

 

模仿一个全局的toast 的显示效果 (ShowAddressService.java)

把自己的一个view 对象 加到窗体上

WindowManager windowManager;

       windowManager = (WindowManager) getApplicationContext()

              .getSystemService(Context.WINDOW_SERVICE);

 

windowManage .addView(两个参数);

view,  要添加的view对象

Params  view要显示的参数

 

模仿系统的Toast 来写 layoutparams

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;

//指定窗体显示的类型

Bad token  unable  to add view

 

 

windowManager .removeView(VIew);

 

// 音乐播放器的歌词显示 , 流量监控的小图标

//视频播放器 画中画    windowmanger

 


 

 

打卡在sd卡 获取手机内存上的数据库

SQLiteDatabase db = SQLiteDatabase.openDatabase("数据库的位置",游标工厂, 数据库以什么模式打开);

 

Db.rawQuery();

Db.execSQL();

 

 


移动某个控件的操作

找到该控件 实现 ontouchlinstener

setOnTouchListener(new OnTouchListener() {

 

}

注意:public boolean onTouch(View v, MotionEvent event) 的返回值

一定要设置为true

设置为true不会消费掉触摸事件 继续传下去

否则 MotionEvent.ACTION_MOVE: 就没法被调用

更改 某个view的 位置

View.layout(l,t,r,b);

 

 

添加ip拨号

抓取系统outgoingcall的广播

getResultData() 获取到电话号码

setResultData() 重新设置里面存放的电话

 

 

 

短信备份(SmsUtil.java)

短息内容提供者的uri 

//uri.parse(string) 把字符串转化成 uri

content://sms/ 所有短信的uri

Context://sms/inbox/

Context://sms/outbox/ 发件箱的数据

Context://sms/draft/ 草稿

 

查询短信数据库里面的数据

    Cursor cursor = cr.query(uri, new String[]{"_id","address","body","date","type"},

              null,

              null,        

              "date desc");

Com.android.provider.telephony  apk里面

 

 

如何写xml文件 (SmsUtil.java)

获取xml的序列化器

       XmlSerializer serializer = Xml.newSerializer();

设置文件的格式和输出流

serializer.setOutput(os, "UTF-8");

//设置文件的编码

serializer.startDocument("UTF-8", true);

//生成xml的头

 

 

serialzer.ednDoucment();

//xml 的尾巴

 

serializer.startTag(null, "sms");

//设置tag

serializer.attribute(命名空间, 名字);

//设置tag的属性

 

 

Listview的美化(applicationinstall.xml)

// 设置listview的选择器  更该某个item被选择时候的背景图片

android:listSelector="@drawable/focused_bg"

//指定item之间的 颜色 和宽度

android:divider="#00ffffff"  "@drawable/image"

android:dividerHeight="3.0dip"

//去掉系统listview的黄色背景        

android:cacheColorHint="#00000000"

 

Popupwindow(AppManagerActivity.java)

//popupwindow的布局 myview

mPopupWindow = new PopupWindow(myview, LayoutParams.WRAP_CONTENT,

              LayoutParams.WRAP_CONTENT);

mPopupWindow.setFocusable(true);

注意:

Drawable localDrawable = getResources().getDrawable(R.drawable.local_popup_bg);

       

 // ********************非常重要,要设置popupwindow的背景

   mPopupWindow.setBackgroundDrawable(localDrawable);

如果没有设置 background 回导致back 键失效

 

显示popupwindow

       mPopupWindow.showAtLocation(view, 51, i, j);

       mPopupWindow.showAsDropDown(anchor)

 

 

销毁popupwindow

mPopupWindow.dismiss() ;

 

设置popupwindow的动画效果

mPopupWindow.setAnimationStyle(R.anim.popup_enter);

 

 

给某个view控件 设置 动画插入器

    // 实例化一个动画效果的插入器 , 插入器是作用在某个view对象上的

    Interpolator mInterpolator = new Interpolator() {

 

       public float getInterpolation(float input) {

           final float inner = (input * 1.55f) - 1.1f;

           return 1.2f - inner * inner;

       }

    };

 

动画插入器 需要设置给一个animation

构建一个 animation

方法1 new Animation();

方法2AnimationUtils.loadAnimation(AppManagerActivity.this, R.anim.popup_enter);

设置animation的插入器

anim.setInterpolator(mInterpolator);

//view控件的 startAnimation()

view.startAnimation(anim);

 

 

启动某个系统的程序 (AppManagerActivity.java)

首先获取到 程序的 packinfo

获取package里面 activity的集合

获取里面有lunch 能力的activity

 

New Component (包名 ,activity的名字);

Intent intent = new Intent(component);

startAcitvity(intent);

 

删除某个程序的操作 (AppManagerActivity.java)

程序的包名

包装成一个uri类型

Package:<包名>

Uri.parse(string);

intent.setAction(Intent.ACTION_DELETE);

Uri data = Uri.parse("package:" + packname);

intent.setData(data);

startActivity(intent);

 

 

分享某个程序 (AppManagerActivity.java)

    Intent localIntent1 = new Intent("android.intent.action.SEND");

localIntent1 = localIntent1.setType("text/plain");

localIntent1 = localIntent1.putExtra(

                     "android.intent.extra.SUBJECT", "f分享");

localIntent1 = localIntent1.putExtra(

                     "android.intent.extra.TEXT", "推荐你使用一款软件  名字为"

                            + packname);

Intent.createChooser(localIntent1, "分享");

startActivity(localIntent1);

 

 

 

程序锁:(LockAppService.java)

获取当前系统的任务栈的信息

Activitymanager  系统一个服务

       // 得到activity的管理器

activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

activityManager.getRunningTasks(做多获取多少条木数量) //获取当前系统正在运行的task的信息

RunningTaskInfo . topActivity

           .bottomActivity;

 

RunningTaskInfo . topActivity..getPackageName();获取到当前activity所在的包名

 

 

 

 

绑定service 使用service的方法

1.需要通过bindservice的方法 去启动service

 

2.通过 ServiceConnecton 获取到ibinder的对象

 

 3.通过ibinder对象去访问服务里面的方法 

 

 

Aidl android interface definition language

 

使程序员能够访问 远程服务里面的方法

需要有aidl的文件

Gen目录生成一个接口文件  

//把代理的ibinder对象转化成接口

接口.Stub.asInterface(service);

 

 

 

创建一个contextmenu (AppManagerActivity.java)

1. 把contextmenu注册给一个view控件

       // 把content menu注册 某个view控件 上

       registerForContextMenu(lv_appmanage);

 

 

2. 初始化contextmenu 

重写  onCreateContextMenu

@Override

    public void onCreateContextMenu(ContextMenu menu, View v,

           ContextMenuInfo menuInfo) {

       // TODO Auto-generated method stub

       super.onCreateContextMenu(menu, v, menuInfo);

      

        MenuInflater inflater = getMenuInflater();

// 获取一个menu的布局填充器

        inflater.inflate(R.menu.context_menu, menu);

//填充menu

 

 

    }

Resource 目录下 menu目录 里面 menu显示的内容

 

3. 获取menu点击事件

onContextItemSelected(MenuItem item)

(AdapterContextMenuInfo) item.getMenuInfo();

AdapterContextMenuInfo 获取当前listview item对应的position

 

 

 

Notification :

1. 获取到系统的notificationManager

 

    notficationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 

 

 2. 实例化一个notification

 

         Notification notification = new Notification(icon, tickerText, when);

 

 

3 .设置用户点击notification的动作

         // pendingIntent 延期的意图

         Intent intent = new Intent(this,MainActivity.class);

        

         PendingIntent pendingIntent  = PendingIntent.getActivity(this, 0, intent, 0);

         // 把一个延期的意图设置给 notification

         notification.setLatestEventInfo(this, "自动ip拨号设置完成", "ip号码为"+ipnumber, pendingIntent);

       

4. 把定义的notification 传递给 notificationmanager

         notficationManager.notify(0, notification);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值