当前位置:  编程技术>移动开发
本页文章导读:
    ▪应用线程跟踪进度条当前进度示例        使用线程跟踪进度条当前进度示例 主要是通过android.os.Handler的消息机制实现控制package com.example.progressdialog; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import androi.........
    ▪ 简略系统的运行的服务        简单系统的运行的服务 public class Main extends Activity { private static String APP_TAG = "tag";  @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);   Act.........
    ▪ 统制toast出现位置以及progressbar的出现消失       控制toast出现位置以及progressbar的出现消失 import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import androi.........

[1]应用线程跟踪进度条当前进度示例
    来源: 互联网  发布时间: 2014-02-18
使用线程跟踪进度条当前进度示例
主要是通过
android.os.Handler
的消息机制实现控制

package com.example.progressdialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationTest extends Activity {
    static final int PROGRESS_DIALOG = 0;
    Button button;
    ProgressThread progressThread;
    ProgressDialog progressDialog;
   
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Setup the button that starts the progress dialog
        button = (Button) findViewById(R.id.progressDialog);
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                showDialog(PROGRESS_DIALOG);
            }
        }); 
    }
   
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case PROGRESS_DIALOG:
            progressDialog = new ProgressDialog(NotificationTest.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("Loading...");
            progressThread = new ProgressThread(handler);
            progressThread.start();
            return progressDialog;
        default:
            return null;
        }
    }

    // Define the Handler that receives messages from the thread and update the progress
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            int total = msg.getData().getInt("total");
            progressDialog.setProgress(total);
            if (total >= 100){
                dismissDialog(PROGRESS_DIALOG);
                progressThread.setState(ProgressThread.STATE_DONE);
            }
        }
    };

    /** Nested class that performs progress calculations (counting) */
    private class ProgressThread extends Thread {
        Handler mHandler;
        final static int STATE_DONE = 0;
        final static int STATE_RUNNING = 1;
        int mState;
        int total;
       
        ProgressThread(Handler h) {
            mHandler = h;
        }
       
        public void run() {
            mState = STATE_RUNNING;   
            total = 0;
            while (mState == STATE_RUNNING) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Log.e("ERROR", "Thread Interrupted");
                }
                Message msg = mHandler.obtainMessage();
                Bundle b = new Bundle();
                b.putInt("total", total);
                msg.setData(b);
                mHandler.sendMessage(msg);
                total++;
            }
        }
        
        /* sets the current state for the thread,
         * used to stop the thread */
        public void setState(int state) {
            mState = state;
        }
    }
}

    
[2] 简略系统的运行的服务
    来源: 互联网  发布时间: 2014-02-18
简单系统的运行的服务

public class Main extends Activity {
 private static String APP_TAG = "tag";

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(50);

  for (int i = 0; i < runningServices.size(); i++) {
   ActivityManager.RunningServiceInfo runningServiceInfo = runningServices.get(i);
   Log.i(APP_TAG, "Process " + runningServiceInfo.process + " with component " + runningServiceInfo.service.getClassName());
  }
 }
}


    
[3] 统制toast出现位置以及progressbar的出现消失
    来源: 互联网  发布时间: 2014-02-18
控制toast出现位置以及progressbar的出现消失
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * Class which shows how to show toast
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {

    private Button clickBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickBtn = (Button) findViewById(R.id.click);
        clickBtn.setText("Show message");
        clickBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast msg = Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG);
                msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
                msg.show();
            }
        });
    }
}

 

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Class which shows how to work with handler class
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {

    private Button clickBtn;
    private ProgressDialog progressDialog;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            progressDialog.dismiss();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickBtn = (Button) findViewById(R.id.click);
        clickBtn.setText("Click me");
        clickBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                processThread();
            }
        });
    }

    private void processThread() {
        progressDialog = ProgressDialog.show(Main.this, "", "Doing...");
        new Thread() {
            public void run() {
                longTimeMethod();
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    private void longTimeMethod() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Log.e("tag", e.getMessage());
        }
    }
}

 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3