Android(六)通知、样式、主题、HTML
一、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:
<!--l version=<-->"1.0" encoding="utf-8"?>
<!-- "changcheng"</em>></div> <div mce_tmp="1"><item name=<em>"android:textSize"</em>>18px</item></div> <div mce_tmp="1"><item name=<em>"android:textColor"</em>>#0000CC</item></div> <div mce_tmp="1"> -->
2).在layout文件中可以通过style或 theme属性设置样式或主题。
三、使用HTML做为UI
使用LayoutUI比较麻烦,不能让美工参与进来,这样就为开发人员带来了麻烦。但我们可以通过HTML+JS来进行UI的设计与操作。
1).在assets添加Html页面
<!--CTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dt-->
编号 | 姓名 | 电话 |
2).在main.xlm中添加一个WebView控件
<!--l version=<-->"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
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添加权限:
<!-- 安装程序权限 -->
"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);
Android的学习到此结束!
更多相关文章
- Android踩坑日记:自定义水平和圆形ProgressBar样式
- (Android)react-native更改状态栏文字和图标颜色
- Android根据包名取得指定程序包的信息(名称、图标……)
- Android应用程序中应用图标和名字的设置
- Android 样式开发(二)selector篇
- ImageButton 和ImageWell 样式的浅析