Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480011
  • 博文数量: 153
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1724
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-08 11:55
文章分类

全部博文(153)

文章存档

2011年(1)

2010年(55)

2009年(88)

2008年(9)

我的朋友

分类: LINUX

2010-12-17 09:56:13


一、Android中的通知

         一般手机上边都有一个状态条,显示电池电量、信 号强度、未接来电、短信...Android的屏幕上方也具有状态条。这里所说的通知,就是在这个状态条 上显示通知。

 

       发送通知的步骤如下:

       1).获取通知管理器

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

       2).新建一个通知,指定其图标 和标题

       int icon = android.R.drawable.stat_notify_chat;

       long when = System.currentTimeMillis();

       //第一个参数为图标,第二个 参数为标题,第三个为通知时间

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

       Intent openintent = new Intent(this, OtherActivity.class);

       //当点击消息时就会向系统发送openintent意图

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);

       notification.setLatestEventInfo(this, “标题”, “内 容", contentIntent);

       mNotificationManager.notify(0, notification);

 

二、Android中的样式和主题

       android中的样式和CSS样式作用 相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控 件属性的集合。如:需要定义字体的颜色和大小。

 

       1).values目录下添加styles.xml:

xml version="1.0" encoding="utf-8"?>

<resources>

         <style name="changcheng">

                   <item name="android:textSize">18pxitem>

        <item name="android:textColor">#0000CCitem>

         style>

resources>

 

       2).layout文件中可以通过styletheme属性设置样式或主题。

 

三、使用HTML做为UI

       使用LayoutUI比较 麻烦,不能让美工参与进来,这样就为开发人员带来了麻烦。但我们可以通过HTML+JS来 进行UI的设计与操作。

 

       1).assets添加Html页面

</span><span style="font-family: 微软雅黑; font-size: 7.5pt;">联系人列表<span xml:lang="EN-US" lang="EN-US">

        

        

                  

                           

                  

        

编 号姓名电话

        

 

       2).main.xlm中添加一个WebView控 件

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

         <WebView

             android:layout_width="fill_parent"

             android:layout_height="fill_parent"

             android:id="@+id/webView"

            />

LinearLayout>

 

       3).Activity

package cn.itcast.html;

 

import java.util.ArrayList;

import java.util.List;

import org.json.JSONArray;

import org.json.JSONObject;

import cn.itcast.domain.Contact;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.webkit.WebView;

 

public class ContactActivity extends Activity {

         private static final String TAG = "ContactActivity";

         private WebView webView;

         private Handler handler = new Handler();

        

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        webView = (WebView)this.findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);//设置支持javaScript

        webView.getSettings().setSaveFormData(false);//不保存表单数据

        webView.getSettings().setSavePassword(false);//不保存密码

        webView.getSettings().setSupportZoom(false);//不支持页面放大功能

        //addJavascriptInterface方 法中要绑定的Java对象及方法要运行在另外的线程中,不能运 行在构造他的线程中

        webView.addJavascriptInterface(new MyJavaScript(), "itcast");

        webView.loadUrl("file:///android_asset/index.html");

    }

    private final class MyJavaScript{

       public void call(final String phone){

                handler.post(new Runnable() {                                  

                                     @Override

                                     public void run() {

                                               Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone));

                                               startActivity(intent);

                                     }

                            });                      

       }

       /**

        * 获取所有联系人

        */

       public void getContacts(){

                handler.post(new Runnable() {                                  

                                     @Override

                                     public void run() {

                                               //可以通过访问SQLLite数据库得到联系人

                                   List contacts = new ArrayList();

                                   contacts.add(new Contact(27, "路飞", "12345"));

                                   contacts.add(new Contact(28, "索隆", "67890"));

                                   String json = buildJson(contacts);

                                   webView.loadUrl("javascript:show('"+ json +"')");

                                     }

                            });                      

       }

       //生 成Json格式的数据

       private String buildJson(List contacts){

                try {

                         JSONArray array = new JSONArray();

                                     for(Contact contact : contacts){

                                               JSONObject item = new JSONObject();

                                               item.put("id", contact.getId());

                                               item.put("name", contact.getName());

                                               item.put("phone", contact.getPhone());

                                               array.put(item);

                                     }

                                     return array.toString();

                            } catch (Exception e) {

                                     Log.e(TAG, e.toString());

                            }

                            return "";

       }

    }

}

      

       MyJavaScript接口实现的方法正是提供给页面中的JS代码调用的!

 

 

四、打包和安装Android应 用

       1.导出Android应用

       在工程上右解-->Export-->Android-->Export Android Application, 将工程导出为APK包。

 

       2.APK包放入到SDCard目录中

       FileExplorer面板的右上角有一个导入手机图标,将上面生成的APK包导入到SDCard目录中。

      

       3.编写安装APK包的Android程 序

       1).AndoirdManifest.xml添加权限:

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

 

       2).通过Android提供的 功能,安装APK

Intent intent = new Intent();

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.setAction(android.content.Intent.ACTION_VIEW);

Uri data = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), filename));

intent.setDataAndType(data, "application/vnd.android.package-archive");

startActivity(intent);

 


阅读(1449) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-17 15:04:56

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com