.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.application3.UIActivity7"
android:orientation="vertical"> <AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="自动完成文本编辑框"
android:id="@+id/actv_1"
android:completionThreshold="1"/>
<!--completionThreshold 提示所需要的字符-->
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sp_1"> </Spinner>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="状态栏提示"
android:onClick="notification_onClick"/> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/da2"
android:id="@+id/iv_3"/>
</LinearLayout>

.java

package com.hanqi.application3;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast; import java.util.Timer;
import java.util.TimerTask; public class UIActivity7 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ui7); AutoCompleteTextView actv_1 = (AutoCompleteTextView)findViewById(R.id.actv_1);
//设置自动完成的数据源 String[] strings = {"China","Child","Chile","Chinese","Add","All","Class","Ddp"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.layout_array,strings); actv_1.setAdapter(arrayAdapter); //下拉列表
String[] strings1 = {"China","Child","Chile","Chinese","Add","All","Class","Ddp"};
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this,R.layout.layout_array,strings1);
Spinner sp1= (Spinner)findViewById(R.id.sp_1); sp1.setAdapter(arrayAdapter1); sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(UIActivity7.this, "position ="+position, Toast.LENGTH_SHORT).show();
} @Override
public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(UIActivity7.this, "什么也没选", Toast.LENGTH_SHORT).show(); }
}); //handler
final ImageView iv3 = (ImageView)findViewById(R.id.iv_3); final int[] iImageid = {R.drawable.da1,R.drawable.da2,R.drawable.da3,R.drawable.da4}; //继承Handler
final Handler handler = new Handler()
{
int i = 0;
//处理消息的回调方法
//重写
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//判断消息的类别
if(msg.what ==1)
{
//切换图片
iv3.setImageResource(iImageid[i++ % iImageid.length]);
// i++;
// if(i==10)
// {
// i=0;
// } }
}
};
//在新线程发送消息
//定式循环发送
//定时器:启动新线程,定时执行代码
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//发送消息
//空消息
handler.sendEmptyMessage(1); }
},1000,2000); }
public void notification_onClick(View v)
{
//1.获取状态栏消息管理器
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //3.构建意图
Intent intent = new Intent(this,UIActivity2.class); //4.获取PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0); //2.构建消息 方法链调用
Notification nt = new Notification.Builder(this)
.setContentTitle("这是一个通知")
.setContentText("这是通知内容:点击打开新的界面 ")
.setTicker("新通知")
.setSmallIcon(R.drawable.da1)//图片
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)//声音
.setContentIntent(pendingIntent) .build();
//交给管理器,发出消息
nm.notify(0,nt); }
}

andorid 多线程handler用法的更多相关文章

  1. C#多线程的用法8-线程间的协作AutoResetEvent

    AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...

  2. Python爬虫进阶五之多线程的用法

    前言 我们之前写的爬虫都是单个线程的?这怎么够?一旦一个地方卡到不动了,那不就永远等待下去了?为此我们可以使用多线程或者多进程来处理. 首先声明一点! 多线程和多进程是不一样的!一个是 thread ...

  3. android中handler用法总结

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  4. Android(java)学习笔记134:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  5. Android学习笔记--Handler用法总结

    不错的例子:http://www.cnblogs.com/menlsh/archive/2013/06/07/3125341.html 转自:一叶知秋的博客 http://blog.sina.com. ...

  6. C#多线程的用法10-线程池

    TheadPool:在进行多线程编程时,如果不想频繁的创建线程,那可以考虑使用使用线程池来完成多线程编程的工作.你只需将要处理的任务交付给ThreadPool,如果ThreadPool中有空闲的线程, ...

  7. Android(java)学习笔记76:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  8. Handler用法总结

    一.线程通讯问题 1.1 Message.Handler.Looper 在Android中提供了一种异步回调机制Handler,我们可以它来完成一个很长时间的任务. Handler基本使用: 在主线程 ...

  9. Handler用法

    1.子线程创建handler 方法一 HandlerThread handlerThread = new HandlerThread(" sub thread name");  / ...

随机推荐

  1. windows7 IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET

    IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET windows7,部署在本地的IIS7里以后,结果不能访问承载SL的.aspx页面,而如果用.html承载则可以访问. 亲测可用修复办法 ...

  2. unity3d assetbundle打包策略

    由于assetbundle打包存在依赖的问题,所有资源要进行合理的分包 零.代码 代码都放在本地,包括NGUI等插件的代码.shader代码(内置的shader无需打包,而自定义的shader还是需要 ...

  3. 闹钟AlarmAndMusic 和支持播放音乐效果《IT蓝豹》

    闹钟AlarmAndMusic 和支持播放音乐效果的,上下滑动调整时间和页面旋转风车效果,由于制作的gif有些问题,效果不明显,欢迎下载使用看看真实的效果.本例子主要由AlertActivity和Al ...

  4. for嵌套

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. Linux下Shell命令加减乘除计算

    使用 expr命令 (其中做乘的时候*号要用斜杠进行转义) 除以 [hadoop-user@hadoop1]$ echo `expr 9 / 3`             3加 [hadoop-use ...

  6. 云主机上配置lamp环境 php5.6+apache2.2.15+mysql5.1.73

    安装 PHP5.6 rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm; rp ...

  7. 2.7、CDH 搭建Hadoop在安装(使用向导设置群集)

    步骤7:使用向导设置群集 完成“ 群集安装”向导后,“ 群集设置”向导将自动启动.以下部分将指导您完成向导的每个页面: 选择服务 分配角色 设置数据库 查看更改 首次运行命令 恭喜! 选择服务 “ 选 ...

  8. linux suse 同步时间

    ntpdate 210.72.145.44 ip为中国(国家授时中心)

  9. Android学习路-Android Studio的工程目录

    说明:下图为一个app的工程目录,如果在res下随便建立文件夹(比如test等名字)是不会显示在工程内的

  10. JAVA高精度模板

    刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...