[转]android中系统服务的调用

Posted by & filed under Uncategorized.

大家好,好久不见,今天在开发中遇到的一个问题给大家分享一下,我先前做了一个音乐播放器,因为播放音乐一般都是用服务来处理的。 当用户点击播放按钮时,音乐在服务中播放,然后用用户退出程序(服务进程还在,音乐还继续),所以妥用户点再次进入应用时,我们播放器要处于播放状态,这里我作了简单的处理,判断这个音乐服务是否开启来完成的。 今天给大家的小例子是列出Android设备中所有启动的服务,及判断某个服务是否开启,具体步骤如下了: 第一步:新建一个Android工程,命名为RunningService。 第二步:修改RunningService.java代码如下:   [java] view plaincopy package com.tutor.runningservice; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.os.Bundle; import android.widget.TextView; public class RunningService extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         //setContentView(R.layout.main);         TextView mTextView = new TextView(this);         ActivityManager mActivityManager =             (ActivityManager)getSystemService(ACTIVITY_SERVICE);         List<ActivityManager.RunningServiceInfo> mServiceList = mActivityManager.getRunningServices(30);         //我要判断的服务名字,我在launcher2里加了一个音乐服务         final String musicClassName = “com.android.launcher2.MusicService”;         boolean b = MusicServiceIsStart(mServiceList, musicClassName);         mTextView.setText(“你要判断的服务状态为: ” +b+”/n” + getServiceClassName(mServiceList));         setContentView(mTextView);     }     //通过Service的类名来判断是否启动某个服务     private boolean MusicServiceIsStart(List<ActivityManager.RunningServiceInfo> mServiceList,String className){         for(int i = 0; i < mServiceList.size(); i ++){             if(className.equals(mServiceList.get(i).service.getClassName())){                 return true;             }         }         return false;     }     //获取所有启动的服务的类名     private String getServiceClassName(List<ActivityManager.RunningServiceInfo> mServiceList){         String res = “”;         for(int i = 0; i < mServiceList.size(); i ++){             res+=mServiceList.get(i).service.getClassName()+ ” /n”;         }         return res;     } }     第三步:运行上述工程,查看效果!   不看不知道,一看吓一跳,乖乖,开了这么多服务,难怪手机很快就没电了。     上面的一些服务好多是系统自带的,并且不可见的,我们可以到Settings->Applications->Running Read more […]

[转]android中线程的使用

Posted by & filed under Uncategorized.

我们都知道Hanlder是线程与Activity通信的桥梁,我们在开发好多应用中会用到线程,有些人处理不当,会导致当程序结束时,线程并没有被销毁,而是一直在后台运行着,当我们重新启动应用时,又会重新启动一个线程,周而复始,你启动应用次数越多,开启的线程数就越多,你的机器就会变得越慢。 为了方便 大家理解,我写一个简单的Demo.功能就是每2秒中将应用的Title更换一次。具体步骤如下: 第一步:新建一个Android工程命名为ThreadDemo。 第二步:修改ThreadDemo.java,代码如下:   [java] view plaincopy package com.tutor.thread; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; public class ThreadDemo extends Activity {     private static final String TAG = “ThreadDemo”;     private int count = 0;     private Handler mHandler =  new Handler();     private Runnable mRunnable = new Runnable() {         public void run() {             //为了方便 查看,我们用Log打印出来             Log.e(TAG, Thread.currentThread().getName() + ” ” +count);             count++;             setTitle(“” +count);             //每2秒执行一次             mHandler.postDelayed(mRunnable, 2000);         }     };     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         //通过Handler启动线程         mHandler.post(mRunnable);     } }   第三步:运行上述工程,查看运行效果:   当然上面不是重点,退出应用时,线程还在跑,打开Logcat视窗或者cmd终端查看,如下(刚吃了午餐回来,线程还在跑着):   当我们再次启动应用时,会重新启动一个新的线程,如下图所示:   所以我们在应用退出时,要将线程销毁,我们只要在Activity中的,onDestory()方法处理一下就OK了,如下代码所示:   [java] view Read more […]

[转]android方法与javascript方法相互调用

Posted by & filed under Uncategorized.

在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用android方法,在这里我个人觉得有点和DWR相似。 为了让大家容易理解,我写了一个简单的Demo,具体步骤如下: 第一步:新建一个Android工程,命名为WebViewDemo(这里我在assets里定义了一个html页面)。 第二步:修改main.xml布局文件,增加了一个WebView控件还有Button控件,代码如下:   [java] view plaincopy <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”     android:orientation=”vertical”     android:layout_width=”fill_parent”     android:layout_height=”fill_parent”     >     <TextView         android:layout_width=”fill_parent”         android:layout_height=”wrap_content”         android:text=”Welcome to Mr Wei’s Blog.”         />     <WebView         android:id=”@+id/webview”         android:layout_width=”fill_parent”         android:layout_height=”wrap_content”     />     <Button         android:id=”@+id/button”         android:layout_width=”fill_parent”         android:layout_height=”wrap_content”         android:text=”Change the webview content”     /> </LinearLayout>   第三步:在assets目录下新建一个demo.html文件,代码如下(这里不知道为何多了mce:这几个东东,<script></script>这样是对的):   [css] view Read more […]

[转]android中解析xml文件的方法

Posted by & filed under Uncategorized.

大家好今天我今天给大家讲解一下android中xml的创建以及一些解析xml的常用方法。 首先是创建,我们用XmlSerializer这个类来创建一个xml文件,其次是解析xml文件,常用的有dom,sax,XmlPullParser等方法,由于sax代码有点复杂,本节只讲解一下dom与XmlPullParser解析,sax我将会在下一节单独讲解,至于几种解析xml的优缺点我就不再讲述了。 为了方便理解,我做了一个简单的Demo。首先首界面有三个按钮,点击第一个按钮会在sdcard目录下创建一个books.xml文件,另外两个按钮分别是调用dom与XmlPullParser方法解析xml文件,并将结果显示在一个TextView里。大家可以按照我的步骤一步步来: 第一步:新建一个Android工程,命名为XmlDemo. 第二步:修改main.xml布局文件,代码如下:   [java] view Read more […]

JSP使用网站访问人数统计功能,方法与技巧

Posted by & filed under Uncategorized.

实现网站访问人数统计功能的步骤: 创建静态登录页面,并指定表单提交由登录处理页面进行处理。 创建登录处理页面获得登录信息,查询数据库,判断该用户是否注册,如果该用户已注册,把已登录用户的信息保存在一个全局范围内。 在新闻发布系统左导航栏中加入超链接,用来导航到右侧的已访问人数统计页面。 创建已访问人数统计页面,从全局范围内取出访问者个数并显示。   需求: 为**系统增加已访问人数统计功能。 关键代码: 1、创建静态登录页面login2.html

2、创建登录处理页面login2.jsp,把已登录用户的信息保存在一个全局范围内

3、在新闻发布系统左导航left.jsp页面中加入超链接

4、创建已访问人数统计页面showAllLoginedUsers.jsp

  实现网站访问人数统计功能的方法和技巧 application对象是全局对象,存放在服务器端,供访问该网站的所有用户共享,所以很耗服务器资源,一般在网站用户访问量较小的情况下使用application对象。 使用application全局变量来存储已访问用户的信息集合,在读取和更改集合时都要对application对象进行强制类型转换。 当重新开启一个浏览器窗口,已访问人数会增加,并在页面上显示出这个用户的信息。 网站一旦因意外重启,则application对象将被重置,计数从0开始,所以通常将统计已访问用户信息存放到数据库中。   不使用实现网站访问人数统计功能的方法和技巧的后果 如果用户访问人数过多,使用application对象来存储已访问用户信息,容易导致服务器负载过重而死机。 使用application对象不进行强制类型转换,会引发程序运行错误。 网站一旦因意外重启,application对象将被重置,计数从0开始,导致以往已访问网站用户的历史记录丢失。 Read more […]

ORA-12154: TNS: 无法解析指定的连接标识符

Posted by & filed under Uncategorized.

相信作为ORACLE数据库的开发人员没有少碰到“ORA-12154: TNS: 无法解析指定的连接标识符”,今天我也又碰到了类似的情况,将我的解决方法进行小结,希望能对碰到同样问题的友人们提供帮助。 问题场景描述: 服务器端版本:oracle 10.2.0.10 客户端版本:oracle 10.2.0.10 客户端开发工具:PL/SQL Developer 通过客户端来连接服务器,其主要文件就是通过客户端的tnsnames.ora来配置,在原文件的基础上增加: SID别名 = (DESCRIPTION =     (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = SERVER_IP)(PORT = 1521))     ) (CONNECT_DATA =       (SID = SID_NAME)     )   ) 例: dev_db = (DESCRIPTION =     (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.168.10.5)(PORT Read more […]

android开发教程21篇(版主强烈推荐,几乎每一篇都是精华教程)

Posted by & filed under Uncategorized.

推荐hellogv 的二十一篇Android开发教程,说句真心话,几乎是每一篇都是精华,值得很多开发者借鉴! Android提高第一篇之MediaPlayer http://www.apkbus.com/android-23947-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第二篇之SurfaceView的基本使用 http://www.apkbus.com/android-23948-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第三篇之SurfaceView与多线程的混搭 http://www.apkbus.com/android-23949-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第四篇之Activity+Intent http://www.apkbus.com/android-23950-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第五篇之Service http://www.apkbus.com/android-23951-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第六篇之BroadcastReceiver http://www.apkbus.com/android-23952-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第七篇之XML解析与生成 http://www.apkbus.com/android-23953-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第八篇之SQLite分页读取 http://www.apkbus.com/android-23954-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第九篇之SQLite分页表格 http://www.apkbus.com/android-23955-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十篇之AudioRecord实现”助听器” http://www.apkbus.com/android-23956-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十一篇之模拟信号示波器 http://www.apkbus.com/android-23957-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十二篇之蓝牙传感应用 http://www.apkbus.com/android-23958-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十三篇之探秘蓝牙隐藏API http://www.apkbus.com/android-23959-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十四篇之探秘TelephonyManager http://www.apkbus.com/android-23960-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十五篇之ListView自适应实现表格 http://www.apkbus.com/android-23961-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高十六篇之使用NDK把彩图转换灰度图 http://www.apkbus.com/android-23962-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高十七篇之多级树形菜单的实现 http://www.apkbus.com/android-23963-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高十八篇之自定义Menu(TabMenu) http://www.apkbus.com/android-23964-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第十九篇之”多方向”抽屉 http://www.apkbus.com/android-23965-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第二十篇之MediaPlayer播放网络音频 http://www.apkbus.com/android-23966-1-1.html <ignore_js_op> ······································································································· ······································································································· ······································································································· Android提高第二十一篇之MediaPlayer播放网络视频 http://www.apkbus.com/android-23967-1-1.html <ignore_js_op> Read more […]

Orcale 11g for Win7(32)

Posted by & filed under Uncategorized.

1、下载Oracle 11g R2 for Windows的版本 下载地址:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 其中包括两个压缩包:win64_11gR2_database_1of2.zip,win64_11gR2_database_2of2.zip   2、安装 将两个压缩包解压到同一个目录下,即“database”,然后单击解压目录下的“setup.exe”文件:   3、配置安全更新 在出现的“配置安全更新”窗口中,取消“我希望通过My Oracle Support接受安全更新”,单击“下一步”:   4、安全选项 在“安装选项”窗口中,选择“创建和配置数据库”,单击“下一步”:   5、系统类 在“系统类”窗口中,选择“桌面类”,单击“下一步”:   6、典型安装 在“典型安装”窗口中,选择Oracle的基目录,选择“企业版”和“默认值”并输入统一的密码为:Oracle11g,单击“下一步”:   7、先决条件检查 在“先决条件检查”窗口中,单击“下一步”:   8、概要 在“概要”窗口中,单击“完成”,即可进行安装:   9、安装产品 出现的安装过程如下: 数据库创建完成后,会出现如下“Database Read more […]