在工作线程中使用Toast

作者:追风剑情 发布于:2016-8-3 15:15 分类:Android

package com.test.androidtest;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		
		mainProcessing();
	}
	
	Handler handler = new Handler();
	
	private void mainProcessing(){
		Thread thread = new Thread(null, doBackgroundThreadProcessing, "Background");
		thread.start();
	}
	
	private Runnable doBackgroundThreadProcessing = new Runnable(){
		@Override
		public void run() {
			backgroundThreadProcessing();
		}
	};
	
	private void backgroundThreadProcessing(){
		handler.post(doUpdateGUI);
	}
	
	// 执行更新GUI方法的Runnable
	private Runnable doUpdateGUI = new Runnable(){
		public void run(){
			Context context = getApplicationContext();
			String msg = "To open mobile development!";
			int duration = Toast.LENGTH_LONG;
			Toast.makeText(context, msg, duration).show();
		}
	};
}

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号