猪头小队长 on March 9th, 2009

废话少说,要实现的效果就是在界面上拖动这一个按钮到处跑。
1. 布局文件

<?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">
<Button android:id="@+id/btn_hello" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

2. 代码

package com.roiding.sample;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class Touch extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button btn = (Button) findViewById(R.id.btn_hello);

btn.setOnTouchListener(new OnTouchListener() {
int[] temp = new int[] { 0, 0 };

public boolean onTouch(View v, MotionEvent [...]

Continue reading about 实现拖动效果

pawa on February 6th, 2009

接着前一部分,我们来讨论一下在具体使用服务时候会碰上一些问题,以及探讨一下解决的办法。
与 Service 通信并且让它持续运行
如果我们想保持和 Service 的通信,又不想让 Service 随着 Activity 退出而退出呢?你可以先 startService() 然后再 bindService()  。当你不需要绑定的时候就执行 unbindService() 方法,执行这个方法只会触发 Service 的 onUnbind() 而不会把这个 Service 销毁。这样就可以既保持和 Service 的通信,也不会随着 Activity 销毁而销毁了。
提高 Service 优先级
Android 系统对于内存管理有自己的一套方法,为了保障系统有序稳定的运信,系统内部会自动分配,控制程序的内存使用。当系统觉得当前的资源非常有限的时候,为了保证一些优先级高的程序能运行,就会杀掉一些他认为不重要的程序或者服务来释放内存。这样就能保证真正对用户有用的程序仍然再运行。如果你的 Service 碰上了这种情况,多半会先被杀掉。但如果你增加 Service 的优先级就能让他多留一会,我们可以用 setForeground(true) 来设置 Service 的优先级。
为什么是 foreground ? 默认启动的 Service 是被标记为 background,当前运行的 Activity 一般被标记为 foreground,也就是说你给 Service 设置了 foreground 那么他就和正在运行的 Activity 类似优先级得到了一定的提高。当让这并不能保证你得 Service 永远不被杀掉,只是提高了他的优先级。
有一个方法可以给你更清晰的演示,进入 $SDK/tools 运行命令

# [...]

Continue reading about 运行后台服务(二)- 使用技巧

pawa on February 6th, 2009

作为一款多任务操作系统,如果不能运行后台服务,显然说不过去,Android 当然提供了运行后台程序的方法。而且非常简单易用,只不过有一些小问题需要注意,这个主题分为两部分,第一部分是如何实现一个Service以及他的生命周期,第二部分是对于一个个后台服务应该注意的事项。我们开始吧!
创建服务类

所谓的服务,在Android里被称做 Service,只要继承 android.app.Service 这个抽象类,并且实现其中几个方法就可以了。

public class RoidingService extends android.app.Service {}

里边必须实现的一个方法是 onBind(Intent intent) ,他具体是做什么的我们下边讲。还有两个重要的回调函数需要覆盖,onCreate() 和 onDestroy()。跟 Actitivty 类似,在创建和销毁 Service 时回调这两个函数,达到初始化或退出前保存状态。
服务的生命周期
有了 Service 类我们如何启动他呢,有两种方法:

Context.startService()
Context.bindService()

在同一个应用任何地方调用 startService() 方法就能启动 Service 了,然后系统会回调 Service 类的 onCreate() 以及 onStart() 方法。这样启动的 Service 会一直运行在后台,直到 Context.stopService() 或者 selfStop() 方法被调用。另外如果一个 Service 已经被启动,其他代码再试图调用 startService() 方法,是不会执行 onCreate() 的,但会重新执行一次 onStart() 。
另外一种 bindService() 方法的意思是,把这个 Service 和调用 Service 的客户类绑起来,如果调用这个客户类被销毁,Service 也会被销毁。用这个方法的一个好处是,bindService() 方法执行后 Service 会回调上边提到的 [...]

Continue reading about 运行后台服务(一)- 生命周期

猪头小队长 on January 4th, 2009

G1的屏幕的分辨率是320×480,虽说不小但也谈不上大,所以有的应用要使用起来,最好还是希望把状态栏(Status Bar)和标题栏(Title Bar)也隐藏了,这样就可以有更大的屏幕空间给应用程序。

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

final Window win = getWindow();
// No Statusbar
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

// No Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.mylayout);
}

摘自anddev

Continue reading about 如何设置隐藏标题栏和状态栏?