当前位置: 编程技术>移动开发
本页文章导读:
▪容易抓取服务器端推送消息的思路 简单抓取服务器端推送消息的思路
这个推送消息的模型就是从Service启动一个线程,定期获取服务器端消息然后显示出来:MessageService.java文件:
package com.text.ac;
import android.app.Notification;
import a.........
▪ view的setTag() 跟 getTag()应用 view的setTag() 和 getTag()应用
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个.........
▪ 创设最简单的Dialog 创建最简单的Dialog
package com.ipjmc.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class SimpleDialogActivity extends Ac.........
[1]容易抓取服务器端推送消息的思路
来源: 互联网 发布时间: 2014-02-18
简单抓取服务器端推送消息的思路
这个推送消息的模型就是从Service启动一个线程,定期获取服务器端消息然后显示出来:
MessageService.java文件:
这个推送消息的模型就是从Service启动一个线程,定期获取服务器端消息然后显示出来:
MessageService.java文件:
package com.text.ac;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class MessageService extends Service {
private Notification mNotification = null;
private NotificationManager mNotifyManager = null;
private Intent mIntent = null;
private PendingIntent mPendingIntent = null;
/** 获取消息线程 */
private MessageThread mMsgThread = null;
private int messageNotificationID = 1000;
public IBinder onBind(Intent intent) {
return null;
}
/** Called by the system when the service is first created. */
@Override
public void onCreate() {
super.onCreate();
mNotification = new Notification();
/**
* The resource id of a drawable to use as the icon in the status bar.
* This is required; notifications with an invalid icon resource will
* not be shown.
*/
mNotification.icon = R.drawable.icon;
mNotification.tickerText = "新消息";
mNotification.defaults = Notification.DEFAULT_SOUND;
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/** 点击跳转的activity. */
mIntent = new Intent(this, ExTextActivity.class);
mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
mMsgThread = new MessageThread();
mMsgThread.isRunning = true;
mMsgThread.start();
}
class MessageThread extends Thread {
public boolean isRunning = true;
public void run() {
while (isRunning) {
try {
Thread.sleep(5000);
/** 获取服务器消息 . */
String mServerMsg = getServerMessage();
if (mServerMsg != null && !"".equals(mServerMsg)) {
/** 更新通知栏. */
mNotification
.setLatestEventInfo(MessageService.this, "新消息",
"您中奖了,1个亿!" + mServerMsg,
mPendingIntent);
mNotifyManager.notify(messageNotificationID,
mNotification);
/** 每次通知完,通知ID递增一下,避免消息覆盖掉. */
messageNotificationID++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onDestroy() {
System.exit(0);
/** 使用System.exit(0),这样进程退出的更干净. */
mMsgThread.isRunning = false;
super.onDestroy();
}
/**
* 这里以此方法为服务器Demo,仅作示例
*
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public String getServerMessage() {
return "YES!";
}
}
[2] view的setTag() 跟 getTag()应用
来源: 互联网 发布时间: 2014-02-18
view的setTag() 和 getTag()应用
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
在写listView 时候要重写BaseAdapter
需要在每个item都加上button 点击button做相应操作
这个时候需要button里面设置下 这行的属性button.setTag(item.user.name);
然后处理button事件。
这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用
Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用
你可以看看android的源码,特别有listview的,你就会发现这个函数很多时候有妙用!呵呵!
我的一点理解是,绑定数据,特别是绑定数据到view。而且可以用getTag()取得,很方便,而且是任意类型的数据,真的很酷。
在实例BaseAdapter()的getView(position, convertView, par)里有用到。convertView这里会用到setTag() getTag()。就可以生成convertView并复用里面的widget
一直觉得这个东西没啥用setTag(),不过只要能用上 就是比较巧的了
在一个程序中呢 我有好多个button 我想点击一个出现1 点击第二个出现2
上面的代码效率不高 而且 无论点击哪一个都会出现21 当然这是我逻辑错误那怎么实现呢
这样呢就实例话一个listener,同时通过tag传值就不是每一个实例都做了
View中的setTag(Onbect)表示给View添加一个格外的数据,以后可以用getTag()将这个数据取出来。
可以用在多个Button添加一个监听器,每个Button都设置不同的setTag。这个监听器就通过getTag来分辨是哪个Button 被按下。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button01);
Button button2 = (Button) findViewById(R.id.Button02);
Button button3 = (Button) findViewById(R.id.Button03);
Button button4 = (Button) findViewById(R.id.Button04);
MyListener listener = new MyListener();
button1.setTag(1);
button1.setOnClickListener(listener);
button2.setTag(2);
button2.setOnClickListener(listener);
button3.setTag(3);
button3.setOnClickListener(listener);
button4.setTag(4);
button4.setOnClickListener(listener);
}
public class MyListener implements View.OnClickListener {
@Override
public void onClick(View v) {
int tag = (Integer) v.getTag();
switch (tag){
case 1:
System.out.println("button1 click");
break;
case 2:
System.out.println("button2 click");
break;
case 3:
System.out.println("button3 click");
break;
case 4:
System.out.println("button4 click");
break;
}
}
}
}
在写listView 时候要重写BaseAdapter
需要在每个item都加上button 点击button做相应操作
这个时候需要button里面设置下 这行的属性button.setTag(item.user.name);
然后处理button事件。
这个东西在一些需要用到Adapter自定控件显示方式的时候非常有用
Adapter 有个getView方法,可以使用setTag把查找的view缓存起来方便多次重用
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) mWidgetsSwitchApp .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.app_gallery_item, null);
vh = new ViewHolder();
vh.view1 = (ImageView) convertView.findViewById(R.id.view1);
vh.view2 = (ImageView) convertView.findViewById(R.id.view2);
vh.view3= (ImageView) convertView.findViewById(R.id.view3);
vh.view4 = (ImageView) convertView.findViewById(R.id.view4);
convertView.setTag(vh);
}else{
vh = (ViewHolder) convertView.getTag();
}
//其他的代码可以直接使用 vh.view1、vh.view2、vh.view3 、vh.view4
}
你可以看看android的源码,特别有listview的,你就会发现这个函数很多时候有妙用!呵呵!
我的一点理解是,绑定数据,特别是绑定数据到view。而且可以用getTag()取得,很方便,而且是任意类型的数据,真的很酷。
在实例BaseAdapter()的getView(position, convertView, par)里有用到。convertView这里会用到setTag() getTag()。就可以生成convertView并复用里面的widget
一直觉得这个东西没啥用setTag(),不过只要能用上 就是比较巧的了
在一个程序中呢 我有好多个button 我想点击一个出现1 点击第二个出现2
for (int i = 0; i < 20; i++) {
cells[i] = (ImageView) findViewById(cellIDs[i]);
cells[cellnumber++].setOnClickListener(new OnClickListener() {
public void onClick(View v){
cellClicked(cellnumber, v);
}
});
}
上面的代码效率不高 而且 无论点击哪一个都会出现21 当然这是我逻辑错误那怎么实现呢
OnClickListener listener = new OnClickListener() {
public void onClick(View v){
int cellId = (Integer) v.getTag(); cellClicked(cellId, v);
}
}
View v;
for (int i = 0; i < 20; i++){
v = findViewById(cellIDs[i]);
v.setOnClickListener(listener);
v.setTag(i);
}
这样呢就实例话一个listener,同时通过tag传值就不是每一个实例都做了
[3] 创设最简单的Dialog
来源: 互联网 发布时间: 2014-02-18
创建最简单的Dialog
package com.ipjmc.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class SimpleDialogActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建并显示Dialog示例
Dialog dialog = new AlertDialog.Builder(SimpleDialogActivity.this)
.setTitle("提示").setMessage("确实要退出程序?") //设置标题
.setPositiveButton("确定", new DialogInterface.OnClickListener() { //添加按钮
//确定按钮的点击事件
@Override
public void onClick(DialogInterface dialog, int which) {
SimpleDialogActivity.this.finish();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() { //添加按钮
//取消按钮的点击事件
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show(); //显示dialog
}
}
最新技术文章: