弹出信息(Toast)
1.默认效果
Toast是一种短暂的提示框,并不需要用户交互,也不会获得focus(焦点),因此可以适合大多数的场景,向用户进行短暂的信息提示。
创建一个Toast很简单,使用Toast的静态方法makeText(Context context, CharSequence text | int resId, int duration),将String(或者String的ID),以及显示的时间长短(LENGTH_SHORT或者LENGTH_LONG)就可以得到一个Toast的对象。
Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();
2.自定义显示位置效果
Toast toast;
toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3在子线程中使用Toast
注意,如果想要在子线程中使用Toast,则必须使用Handler!因为在子线程中不允许主动使用Toast。
关于Handler,详见http://blog.csdn.net/theworldsong/article/details/9118415 。
package com.plusjun.hello3;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button bt1;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
if(bundle.getInt("123") == 1){
Toast.makeText(getApplicationContext(), "子线程出来的Toast",Toast.LENGTH_SHORT).show();
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button)findViewById(R.id.bt1);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(new Runnable() {
public void run() {
Message msg = new Message();
Bundle b = new Bundle();
b.putInt("123", 1);
msg.setData(b);
MainActivity.this.handler.sendMessage(msg);
}
}).start();
}
});
}
}
下拉菜单通知(Notification)
也就是如同微博、QQ在任务栏提示你的那种功能。
所有说明均在注释中注明。
private void addNotificaction() {
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// 创建一个Notification
Notification notification = new Notification();
// 设置显示在手机最上边的状态栏的图标
notification.icon = R.drawable.picture;
// 当当前的notification被放到状态栏上的时候,提示内容
notification.tickerText = "注意了,我被扔到状态栏了";
//DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
//DEFAULT_LIGHTS 使用默认闪光提示
//DEFAULT_SOUND 使用默认提示声音
//DEFAULT_VIBRATE 使用默认手机震动
//不过貌似在高版本API中只能使用DEFAULT_SOUND(反正在Android4.1就是这样),而且震动好像要权限android.permission.VIBRATE
//记得要用位运算叠加
notification.defaults |= Notification.DEFAULT_SOUND;
//audioStreamType的值必须AudioManager中的值,代表着响铃的模式
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
//创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条是出发的activity,所以采用的是PendingIntent
//第三个参数Intent中的第二个参数,是你要点击后跳转的Activity
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity2.class), PendingIntent.FLAG_ONE_SHOT);
// 点击状态栏的图标出现的提示信息设置,第二个参数为主标题,第三个参数为内容,第四个参数为构造好的PendingIntent
notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);
//flags定位:
//FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉/点击清除
//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
notification.flags = notification.FLAG_ONGOING_EVENT;
//启用
manager.notify(1, notification);
//该方法用于清除通知栏消息,对应的通知编号为notify中的第一个参数。该方法一般不用。
//manager.cancel(1);
}
以上部分内容转载或参考来源如下:
http://blog.csdn.net/flowingflying/article/details/8623067
http://blog.csdn.net/chenzheng_java/article/details/6249357
http://blog.csdn.net/joebaby_/article/details/7974094
http://blog.csdn.net/ericyelai/article/details/6213046
http://blog.csdn.net/qinjuning/article/details/6915482
在此表示感谢。
转载请注明来源,版权归原作者所有,未经同意严禁用于任何商业用途。
微博:http://weibo.com/theworldsong
邮箱:theworldsong@foxmail.com