标签:UI 发送 manager tor system contex priority alt source
1.基本用法
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" android:onClick="info" />
MainActivity,按钮事件
public void info(View view){ NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notification= new NotificationCompat.Builder(this) //通知标题 .setContentTitle("通知") //通知正文 .setContentText("美国又欺负叙利亚了") //通知时间? .setWhen(System.currentTimeMillis()) //小图标 .setSmallIcon(R.mipmap.ic_launcher) //大图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .build(); //推送通知,参数1:要保证每个通知的id不同,参数2:Notification对象 manager.notify(1,notification); }
结果
//通知的重要程度,max立即显示...测试后不管用,, .setPriority(NotificationCompat.PRIORITY_MAX)
点击通知
创建一个新的活动和布局,Main2Activity
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v4.app.NotificationCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void info(View view){ Intent intent=new Intent(this,Main2Activity.class); //点击通知后打开指定界面,参数1:上下文,参数2:通常固定是0,参数3:意图,参数4:行为,通常传0 PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notification= new NotificationCompat.Builder(this) //通知标题 .setContentTitle("通知") //通知正文 .setContentText("美国又欺负叙利亚了") //通知时间? .setWhen(System.currentTimeMillis()) //小图标 .setSmallIcon(R.mipmap.ic_launcher) //大图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) //接收一个PendingIntent对象 .setContentIntent(pi) .build(); //推送通知,参数1:要保证每个通知的id不同,参数2:Notification对象 manager.notify(1,notification); } }
标签:UI 发送 manager tor system contex priority alt source
原文地址:https://www.cnblogs.com/buchizaodian/p/8832603.html