简单的小问题,如果列出了所有安装的程序,怎么样打开该程序呢?

要调用该程序,有两个必要因素:packageName, className
比如打开计算器程序:

Intent i = new Intent();
i.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivity(i);


前面是packageName,后面是className,我们尝试取出此两个值即可。

List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
for(int i=0;i<packs.size();i++) {
  PackageInfo p = packs.get(i);
  //p.packageName;
  //p.activities[0].name; //(className)
}
Tags: ,
代码非原创,fix了bug,完善的还是需要再思量:
/**
* 通过拼接的方式构造请求内容,实现参数传输以及文件传输
* @param actionUrl
* @param params
* @param files
* @return
* @throws IOException
*/
public static String post(String actionUrl, Map<String, String> params,
    Map<String, File> files) throws IOException {

  String BOUNDARY = java.util.UUID.randomUUID().toString();
  String PREFIX = "--" , LINEND = "\r\n";
  String MULTIPART_FROM_DATA = "multipart/form-data";
  String CHARSET = "UTF-8";

  URL uri = new URL(actionUrl);
  HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
  conn.setReadTimeout(5 * 1000); // 缓存的最长时间
  conn.setDoInput(true);// 允许输入
  conn.setDoOutput(true);// 允许输出
  conn.setUseCaches(false); // 不允许使用缓存
  conn.setRequestMethod("POST");
  conn.setRequestProperty("connection", "keep-alive");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

  // 首先组拼文本类型的参数
  StringBuilder sb = new StringBuilder();
  for (Map.Entry<String, String> entry : params.entrySet()) {
    sb.append(PREFIX);
    sb.append(BOUNDARY);
    sb.append(LINEND);
    sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
    sb.append("Content-Type: text/plain; charset=" + CHARSET+LINEND);
    sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
    sb.append(LINEND);
    sb.append(entry.getValue());
    sb.append(LINEND);
  }

  DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
  outStream.write(sb.toString().getBytes());
  // 发送文件数据
  if(files!=null){
    int i = 0;
    for (Map.Entry<String, File> file: files.entrySet()) {
      StringBuilder sb1 = new StringBuilder();
      sb1.append(PREFIX);
      sb1.append(BOUNDARY);
      sb1.append(LINEND);
      sb1.append("Content-Disposition: form-data; name=\"file"+(i++)+"\"; filename=\""+file.getKey()+"\""+LINEND);
      sb1.append("Content-Type: application/octet-stream; charset="+CHARSET+LINEND);
      sb1.append(LINEND);
      outStream.write(sb1.toString().getBytes());

      InputStream is = new FileInputStream(file.getValue());
      byte[] buffer = new byte[1024];
      int len = 0;
      while ((len = is.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
      }

      is.close();
      outStream.write(LINEND.getBytes());
    }
  }
  
  //请求结束标志
  byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
  outStream.write(end_data);
  outStream.flush();

  //得到响应码
  int res = conn.getResponseCode();
  InputStream in = null;
  if (res == 200) {
    in = conn.getInputStream();
    int ch;
    StringBuilder sb2 = new StringBuilder();
    while ((ch = in.read()) != -1) {
      sb2.append((char) ch);
    }
  }
  return in == null ? null : in.toString();
}
Tags: ,
Dnsmasq是一个很实用的小工具,解决局域网的需求看来非常合适,特别是网关和防火墙上。

它可以提供如下几个实用的功能:
1 提供dns服务
2 优先使用本地自定义dns
3 提供dhcp服务

一般情况下,我们可以用bind解决dns的问题,dhcpd解决dhcp的问题,另外,还可以用ypbind解决自定义hostname解析的ip(当然还有用户的功能),它都解决了!很实用吧?这真的很吸引人,况且它一直在更新维护,最新版本是6月份的。

安装过程很简单:

wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.55.tar.gz
tar xfz dnsmasq-2.55.tar.gz
cd dnsmasq-2.55
make
make install
cp dnsmasq.conf.example /etc/dnsmasq.conf


运行起来就可以解决第一个问题:

[root@aslibra dnsmasq-2.55]# dnsmasq
[root@aslibra dnsmasq-2.55]# netstat -nlp|grep dnsmasq
tcp        0      0 0.0.0.0:53                  0.0.0.0:*                   LISTEN      25584/dnsmasq      
tcp        0      0 :::53                       :::*                        LISTEN      25584/dnsmasq      
udp        0      0 0.0.0.0:53                  0.0.0.0:*                               25584/dnsmasq      
udp        0      0 :::53                       :::*                                    25584/dnsmasq      


默认情况下,作为dns服务,第一个问题解决了。

我们尝试解决第二个问题,可以说是dns劫持?没有那么严重,用好了其实很实用。
有如下几个问题比较棘手:

1 局域网有很多机器希望使用一份一样的hosts定义一批名称对应的ip,你需要经常维护这份列表
2 你希望局域网的人访问某个域名时,拦截下来到指定的ip,做缓存节省带宽或者其它用途都可以
3 禁止某个域名的正常解析

这个更改涉及两个文件:
1 /etc/hosts
2 /etc/dnsmasq.conf

我们先在hosts文件里加入两行:

192.168.1.3     server1
192.168.1.2     www.aslibra.com


编辑dnsmasq.conf,找到如下代码:

# Add local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
local=/localnet/

# Add domains which you want to force to an IP address here.
# The example below send any host in doubleclick.net to a local
# webserver.
#address=/doubleclick.net/127.0.0.1
address=/baidu.com/127.0.0.1


重启dnsmasq即可,下面我们在局域网另外一个机器用dig命令测试一下:
Tags: , ,
目的:对某些用户进行登录ip限制

proftp配置:
限制以下两个用户只能从某两个ip段登录

<IfUser user1 user2>
  <Limit LOGIN>
    Order allow,deny
    Allow from 1.2.3.0/24 2.3.4.0/24
    DenyAll
  </Limit>
</IfUser>


默认情况下proftp没有这个模块,会提示出错:

Checking syntax of configuration file
- Fatal: unknown configuration directive '<IfUser>' on line 24 of '/Data/apps/proftpd/etc/proftpd.conf'


从源码编译,可以选择编译进来的,比如可以类似这样:

./configure --prefix=/Data/apps/proftpd \
--enable-shadow \
--enable-autoshadow   \
--with-modules=mod_ratio:mod_readme:mod_wrap:mod_ifsession \
&& make && make install


参考:http://blog.chinaunix.net/u/6098/showart_237162.html
Tags: , ,
android有处理鼠标动作的GestureDetector,使用起来也挺方便,几个常用事件你都可以找到接口接入处理:

单击、双击(比如双击放大)、滑动(比如翻页)
下面是简化的例子,参考并整合所得,来源参考页尾参考内容:

package com.aslibra.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class touchGesture extends Activity implements OnGestureListener {      
  private LinearLayout main;      
  private TextView viewA;
  private GestureDetector gestureScanner;

  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  

    gestureScanner = new GestureDetector(this);  
    gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener(){  
      public boolean onDoubleTap(MotionEvent e) {  
        viewA.setText("-" + "onDoubleTap" + "-");  
        //双击时产生一次
        Log.v("test", "onDoubleTap");
        return false;  
      }
      public boolean onDoubleTapEvent(MotionEvent e) {  
        //双击时产生两次
        Log.v("test", "onDoubleTapEvent");
        return false;
      }  
      public boolean onSingleTapConfirmed(MotionEvent e) {  
        viewA.setText("-" + "onSingleTapConfirmed" + "-");  
        //短快的点击算一次单击
        Log.v("test", "onSingleTapConfirmed");
        return false;  
      }  
    });


    main = new LinearLayout(this);  
    main.setBackgroundColor(Color.GRAY);
    main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
    main.setOrientation(LinearLayout.VERTICAL);

    viewA = new TextView(this);
    viewA.setBackgroundColor(Color.YELLOW);
    viewA.setTextColor(Color.BLACK);  
    viewA.setTextSize(16);
    viewA.setLayoutParams(new LinearLayout.LayoutParams(320,50));  
    main.addView(viewA);

    setContentView(main);  
  }

  @Override  
  public boolean onTouchEvent(MotionEvent me) {  
    return gestureScanner.onTouchEvent(me);  
  }  

  @Override  
  public boolean onDown(MotionEvent e) {  
    //viewA.setText("-" + "DOWN" + "-");  
    return true;  
  }  

  @Override  
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
    //viewA.setText("-" + "FLING" + "- "+velocityX + "- "+velocityY);  
    Log.v("test", "onFling "+e1.getX()+" "+e2.getX());
    return true;  
  }  

  @Override  
  public void onLongPress(MotionEvent e) {  
    //viewA.setText("-" + "LONG PRESS" + "-");  
  }  

  @Override  
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
    //viewA.setText("-" + "SCROLL" + "- "+distanceX + "- "+distanceY);  
    Log.v("test", "onScroll "+e1.getX()+" "+e2.getX());
    return true;  
  }  

  @Override  
  public void onShowPress(MotionEvent e) {  
    //viewA.setText("-" + "SHOW PRESS" + "-");  
  }

  @Override  
  public boolean onSingleTapUp(MotionEvent e) {  
    Log.v("test", "onSingleTapUp");
    //viewA.setText("-" + "SINGLE TAP UP" + "-"+ i++);  
    return true;  
  }  
  
}  


参考资料:

1 GestureDetector and GestureDetector.OnGestureListener
2 how to implement android double tap?
Tags:
分页: 3/53 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页 [ 显示模式: 摘要 | 列表 ]

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐