您的位置:首页 > 移动开发 > Android开发

Android中的消息通知Toast和Notification

2015-07-26 12:03 656 查看

Android中的消息通知Toast和Notification

1.弹出通知Toast

MainActivity.java

package 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.java

package 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" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: