首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android Service的思考(3)

前两篇文章中可以看到Activity和Service,context.startService对应着Service中的onStart()方法,context.onBindService对应的是Service中的onBind()方法。当我们继想绑定一个Service又想在Activity停止时,Service不会停止,我们可以先StartService,然后再BindService()。这时候的流程图如下所示:点击放大 此时需要注意一个问题,当Activity退出的时候,Sercvice并不会停止,此时我们可以再进入Activity重新绑定,当这时候Service就会调用onRebind()方法,但是调用onRebind()方法的前提是先前的onUnbind()方法执行成功,但是使用super.onUnbind(intent)是执行不成功的,这时候我们要手动的使其返回true,再次绑定时Rebind()就会执行。否则,如果退出时不显示的指定onUnbind()为成功的话(为false),那么重新启动此Activity来绑定服务时,Service的onBind()方法和onReBind都不会执行,但是ServiceConnection方法确一定会回调了。这说明在Service中的onBind()方法不同于onStart()方法不能被重复调用。 本文转自 最牛傻蛋 51CTO博客,原文链接:http://blog.51cto.com/zuiniuwang/718311,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android:自定义ContentProvider

1.创建一个类继承SQLiteOpenHelper(若使用SQLite): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MySQLiteHelper extends SQLiteOpenHelper { //重写构造方法 public MySQLiteHelper(Contextcontext,Stringname,CursorFactoryfactory, int version) { super (context,name,factory,version); } //创建表 @Override public void onCreate(SQLiteDatabasedb) { db.execSQL( "CREATETABLEperson(personidINTEGERPRIMARYKEYAUTOINCREMENT,nameVARCHAR(20),phoneVARCHAR(20))" ); } //升级表(当Database的Version低于当前new里的Version,直接执行下面方法) @Override public void onUpgrade(SQLiteDatabasedb, int oldVersion, int newVersion) { int v=newVersion-oldVersion; if (v> 0 ) { db.execSQL( "ALTERTABLEpersonADDsalary3VARCHAR(20)" ); } } } 2.创建自定义ContentProvider: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MyContentProvider extends ContentProvider { private MySQLiteHelpermDBHelper; public MyContentProvider() { } @Override public int delete(Uriuri,Stringselection,String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.delete( "person" ,selection,selectionArgs); return i; } @Override public StringgetType(Uriuri) { throw new UnsupportedOperationException( "Notyetimplemented" ); } //创建Uri private static final UriURI=Uri.parse( "content://com.example.cpone" ); @Override //插入 public Uriinsert(Uriuri,ContentValuesvalues) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); long id=db.insert( "person" , null ,values); return ContentUris.withAppendedId(URI,id); //相当于原先的Uri加上插入的行号 } @Override public boolean onCreate() //创建表 { mDBHelper= new MySQLiteHelper(getContext(), "person" , null , 1 ); return false ; } @Override //查询 public Cursorquery(Uriuri,String[]projection,Stringselection, String[]selectionArgs,StringsortOrder) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); Cursorcursor=db.query( "person" ,projection,selection,selectionArgs, null , null ,sortOrder); return cursor; } @Override //修改 public int update(Uriuri,ContentValuesvalues,Stringselection, String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.update( "person" ,values,selection,selectionArgs); return i; } } 3.在另一个项目里使用自定义ContentProvider: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MyContentProvider extends ContentProvider { private MySQLiteHelpermDBHelper; public MyContentProvider() { } @Override public int delete(Uriuri,Stringselection,String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.delete( "person" ,selection,selectionArgs); return i; } @Override public StringgetType(Uriuri) { throw new UnsupportedOperationException( "Notyetimplemented" ); } //创建Uri private static final UriURI=Uri.parse( "content://com.example.cpone" ); @Override //插入 public Uriinsert(Uriuri,ContentValuesvalues) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); long id=db.insert( "person" , null ,values); return ContentUris.withAppendedId(URI,id); //相当于原先的Uri加上插入的行号 } @Override public boolean onCreate() //创建表 { mDBHelper= new MySQLiteHelper(getContext(), "person" , null , 1 ); return false ; } @Override //查询 public Cursorquery(Uriuri,String[]projection,Stringselection, String[]selectionArgs,StringsortOrder) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); Cursorcursor=db.query( "person" ,projection,selection,selectionArgs, null , null ,sortOrder); return cursor; } @Override //修改 public int update(Uriuri,ContentValuesvalues,Stringselection, String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.update( "person" ,values,selection,selectionArgs); return i; } } 注: 1.ContentProvider仍在主线程上运行,但对他的调用是异步的,不使用消息队列。 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1217552,如需转载请自行联系原作者

优秀的个人博客,低调大师

android107 指针入门

#include <stdio.h> #include <stdlib.h> //指针就是内存地址 //32为操作系统最大是4G内存,32为系统则是2的32次方, //所以只能表示2的32次方个内存地址, 也就是2的32次方个字节就是4G. main(){ int i;//i是内存空间的别名 ,i里面保存的是内存地址, i = 3; printf("%#x\n", &i); system("pause"); } #include <stdio.h> #include <stdlib.h> main(){ //野指针:没有赋值的指针 int* p; int i; double d = 3.14; p = &d; //不是野指针了, //*p = 23; printf("%#x\n", p);//打印地址,p里面存的是地址 printf("%lf\n", *p); system("pause"); } /*##*的三种用法 1. 乘法 2. int* p:定义一个指针变量p,p中存放一个内存地址,这个地址所存放的数据规定是int型 3. *p:取出p中保存的内存地址存放的数据 ##数据传递 * 所有语言所有平台,都只有值传递,引用传递传递的值是内存地址 */ #include <stdio.h> #include <stdlib.h> void swap(int* p, int* q){ int temp = *p; *p = *q; *q = temp; } main(){ int i = 3; int j = 5; printf("i=%d\n", i); printf("j=%d\n", j); swap(&i, &j); printf("i=%d\n", i); printf("j=%d\n", j); system("pause"); } #include <stdio.h> #include <stdlib.h> void function(int* p, int* q){ *p += 15; *q += 15; } main(){ int i = 10; int j = 20; function(&i, &j); printf("%d\n", i); printf("%d\n", j); system("pause"); } #include <stdio.h> #include <stdlib.h> void function(int** p){ int i = 3; printf("i的地址为%#x\n", &i); //p就是取出p内存空间的值 //*p:就是取出p内存空间值的内存空间的值, *p = &i;//修改P所指向的内存空间值的内存空间的值 } main(){ int* mainp;//mainp在栈中存的是地址,指向堆中的一个int数据 的内存空间 。 function(&mainp);//&mainp在栈中存的是地址的地址也就是 mainp的地址,也就是指向mainp的内存空间。 printf("主函数中获取i的地址为%#x\n", mainp); //数据幻影 printf("主函数中获取i的值为%d\n", *mainp);//-2因为i是局部变量已经销毁了 system("pause"); } 内存分析:0级指针,一级指针,二级指针都有, 二级指针是曾曾用名,一级指针是曾用名。 #include <stdio.h> #include <stdlib.h> main(){ //char arr[] = "hello"; int arr[5] = {1,2,3,4,5}; printf("%#x\n", &arr[0]);//0x22ff50 printf("%#x\n", &arr[1]); printf("%#x\n", &arr[2]); printf("%#x\n", &arr[3]); //arr = printf("数组名字的地址%#x\n", &arr);//也是输出第0个元素的地址 printf("数组名字的地址%#x\n", arr);//也是输出第0个元素的地址 //char* p = &arr; int* p = &arr; //+1表示向右偏移一个单位 printf("%d\n", *(p+0));// 1 printf("%d\n", *(p+1));//2 printf("%d\n", *(p+2));//3 printf("%d\n", (p+2)-p); //2 system("pause"); } #include <stdio.h> #include <stdlib.h> main(){ char* cp; int* ip; short* lizhip; int i; char c; cp = &c; ip = &i; //指针的长度都是4个字节 printf("%d\n", sizeof(cp));//4 printf("%d\n", sizeof(ip));//4 printf("%d\n", sizeof(lizhip));//4 printf("%d\n", (p+2)-p); //2,偏移量。 printf("%d\n", cp-ip);//出错, 连续内存空间相加减是可以的,非连续的内存空间相加减是没有意义的, system("pause"); } 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4979472.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

android108 内存分配

#include <stdio.h> #include <stdlib.h> #include <malloc.h> //包含头文件 main(){ int i = 3;//栈中 int arr[10];//栈中 int* p = malloc(sizeof(int) * 10);//p在栈中,malloc在堆里面长度为10个整型,malloc出来的内存是连续的。 *p = 10; *(p+1) = 20; printf("%#x\n", p);//0x3e3cf0 printf("%d\n", *p);//10 printf("%d\n", *(p+1));//20 printf("%d\n", p+1);//4078836 printf("%d\n", *(p+2));//0 printf("%d\n", *(p+3));//0 free(p); system("pause"); } #include <stdio.h> #include <stdlib.h> main(){ int i = 3; int* p = &i; int** q = &p; int*** l = &q; printf("i的值为%d\n", ***l);//3 //l表示取出l内存空间的值,*l表示取出l内存空间值的内存空间的值,**l表示取出l内存空间值的内存空间值的内存空间的值。 system("pause"); } #include <stdio.h> #include <stdlib.h> #include <malloc.h> main(){ printf("请输入学生人数:"); int count; scanf("%d", &count); //int b在C中是系统在栈中申请内存,系统负责回收的。 // malloc申请的是堆内存由程序员释放。 //栈内存只有2M,连续的内存空间,先进来的后释放。 堆的内存和物理内存一样大, //堆不连续是用链表表示的,链表查找比连续的麻烦但是增删比连续的快。 //C是面向过程的没有对象概念所以没有new,malloc就是new。 //静态内存是栈上分配的,编译的时候就已经分配好了,程序的怎个运行期间都存在, //例如全局变量和static变量。动态内存是堆上malloc或者new分配的。 //根据学生人数去动态申请堆内存 , int* p = malloc(sizeof(int) * count);//count个int类型 int i; for(i = 0; i < count; i++){ printf("请输入第%d个学生学号:", i); scanf("%d", p+i); } printf("请输入新增的学生人数:"); int newCount; scanf("%d", &newCount); p = realloc(p, sizeof(int) * (count + newCount));//扩充p内存 for(i = count; i < count + newCount; i++){ printf("请输入第%d个学生学号:", i); scanf("%d", p+i); } for(i = 0; i < count + newCount; i++){ printf("第%d个学生的学号是:%d\n", i, *(p+i)); } system("pause"); } 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/4985285.html,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册