packagecom.example.MainActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private Context mContext;
Bitmap LargeBitmap = null;
private NotificationManager mNManager;
private Notification.Builder mBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
//创建大图标的Bitmap
LargeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ts);
mNManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public void ts(View view){
//跳转到下一个activity中
// Intent intent = new Intent(mContext, LoadActivity.class);//
PendingIntent pit = PendingIntent.getActivity(mContext, 0, intent, 0);
//设置图片,通知标题,发送时间,提示方式等属性
mBuilder = new Notification.Builder(this);
// mBuilder.setSubText("——");
//内容下面的文字
// mBuilder.setTicker(".");
//收到信息后状态栏显示的文字信息
mBuilder.setWhen(System.currentTimeMillis());
//设置通知时间
mBuilder.setSmallIcon(R.drawable.ts);
//设置小图标
mBuilder.setLargeIcon(LargeBitmap);
//设置大图标
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
//设置默认的三色灯与振动器
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
//获取默认铃声
// mBuilder.setAutoCancel(true);
//跳到另外一个页面
// mBuilder.setContentIntent(pit);
Notification notify1 = mBuilder.build();
//通知标题
mBuilder.setContentTitle("呵呵呵...");
mNManager.notify(0x110, notify1);
//添加一个进度条
mBuilder.setProgress(100,0,false);
//给一个子线程
new Thread(){
@Override
public void run() {
super.run();
for (int i=1;i<=100;i++){
mBuilder.setProgress(100,i,false);
mNManager.notify(0x110, mBuilder.build());
mBuilder.setContentText("下载:"+i+"%");
try {
//模拟下载
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//下载完成后更改标题
mBuilder.setContentTitle("开始安装");
mBuilder.setContentText("正在安装中...");
//设置进度条
mBuilder.setProgress(0,0,true);
mBuilder.build().flags=Notification.FLAG_NO_CLEAR;
mNManager.notify(0x110, mBuilder.build());
}
}.start();
}
}