Android中的消息通知Toast和Notification
2015-07-26 12:03
656 查看
Android中的消息通知Toast和Notification
1.弹出通知Toast
MainActivity.javapackage com.example.toast; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private Button showToast,showLongToast,showImageToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showToast=(Button) findViewById(R.id.showToast); showLongToast=(Button) findViewById(R.id.showLongToast); showImageToast=(Button)findViewById(R.id.showImageToast); showToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 Toast ashort=Toast.makeText(MainActivity.this, "显示一个简短的Toast", Toast.LENGTH_SHORT); ashort.setGravity(Gravity.CENTER, 100, -200);//设置偏移量 ashort.show(); } }); showLongToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 Toast.makeText(MainActivity.this, "显示一个较长的Toast", Toast.LENGTH_LONG).show(); } }); showImageToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 Toast ImageToast=Toast.makeText(MainActivity.this, "显示一个带有图片的Toast", Toast.LENGTH_SHORT); ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(R.drawable.ic_launcher); ImageToast.setView(imageView); ImageToast.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.xml
<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" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/showToast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/_toast" /> <Button android:id="@+id/showLongToast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示一个较长的Toast" /> <Button android:id="@+id/showImageToast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="showImageToast" /> </LinearLayout>
2.状态栏提示Notification
MainActivity.javapackage com.example.notification; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.content.Context; import android.support.v4.app.NotificationCompat; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button BNotification; public static final int NOFIFICATION_ID=2000; private int conter=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BNotification=(Button) findViewById(R.id.button1); BNotification.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { conter++; // TODO 自动生成的方法存根 android.support.v4.app.NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentText("哇!你有"+conter+"个新校息!"); builder.setContentTitle("Hello Notification!"); Notification notification=builder.build(); NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(NOFIFICATION_ID, notification); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
activity_main.xml
<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:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/_notification" />
相关文章推荐
- Android Studio导入项目
- android学习笔记-读取手机联系人
- 百度云消息推送机制在即时通信聊天界面的信息处理,技巧篇。
- 安卓MP3播放器开发实例(3)之进度条和歌词更新的实现
- Android四大组件之activity
- 【Intent】Android系统Intent使用大全(持续更新中...)
- Mac Android Studio SVN
- Android TextView 支持BBCode的显示
- 我的Android进阶之旅------>Android二级ListView列表的实现
- Android中SensorManager.getRotationMatrix函数:计算出旋转矩阵,
- Android TextView Html ImageGetter 图片位置不对
- Android和JS代码互相调用
- Android中保存和恢复Fragment状态的最好方法
- Android四大组件及意图和意图过滤器
- Android中Service(服务)详解
- 【攻克Android (11)】适配器视图与适配器
- Android_Audio_Omap_HAL
- Android开发笔记-----activity简述
- 优化Android Studio/Gradle构建
- Android向本地写入一个XML文件和解析XML文件