天天看点

android onclick传递参数,Android中点击通知栏跳转并传值

Intent intent=new Intent(MainActivity.this,NotificationActivity.class);

String str = (String) msg.obj;

Log.i("eeeee",str);

intent.putExtra("ee",str);

//参数:1:Context 2:一般不用 通常传入0 3:Intent 4:FLAG_CANCEL_CURRENT(),FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT

//PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);//延迟跳转

int notifyId = (int) System.currentTimeMillis();

PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, notifyId, intent, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.FLAG_UPDATE_CURRENT

NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification=new NotificationCompat.Builder(MainActivity.this)

.setContentTitle("紧急通知:")

.setContentText((String) msg.obj)

// .setContentText("圣诞节来了")//显示长文本时,后面省略号代替了

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))

.setContentIntent(pendingIntent)//设置可点击跳转

//.setAutoCancel(true)//点击后自动取消通知(方法1)

// .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))//设置声音

.setVibrate(new long[] {0, 1000, 1000, 1000})//使震动 数组表示 静止0秒,震动1秒 静止1秒 震动1秒

//声明震动的权限

.setLights(Color.GREEN, 1000, 1000)//设置呼吸灯 参数:颜色 亮起时长 暗去时长

.setDefaults(NotificationCompat.DEFAULT_ALL)//设置默认

//.setStyle(new NotificationCompat.BigTextStyle().bigText(message))//显示长文本

// .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))//设置显示大图片

.setPriority(NotificationCompat.PRIORITY_MAX)//设置重要程度: PRIORITY_DEFAULT (表示默认)PRIORITY_MIN(表示最低) PRIORITY_LOW (较低)PRIORITY_HIGH (较高)PRIORITY_MAX(最高)

.build();

manager.notify(1,notification);

public class NotificationActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.notification_layout);

TextView tv_text = (TextView) findViewById(R.id.tv_text);

Intent intent = getIntent();

String str = intent.getStringExtra("ee");

Log.i("ssssssssss",str);

tv_text.setText(str);

//通知栏,取消通知(方法2)

NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

manager.cancel(1);

}

}