主java:
package com.example.administrator.myapplication932; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; public class MainActivity extends Activity { final int NOTIFYID=0x123; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final NotificationManager notificationManager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification.Builder notification=new Notification.Builder(this); notification.setAutoCancel(true); notification.setSmallIcon(R.drawable.packet); notification.setContentTitle("奖励百万红包!!!"); notification.setContentText("点击查看详情!"); notification.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE); notification.setWhen(System.currentTimeMillis()); Intent intent=new Intent(MainActivity.this, Detail.class); PendingIntent pi=PendingIntent.getActivity(MainActivity.this,0,intent,0); notification.setContentIntent(pi); notificationManager.notify(NOTIFYID,notification.build()); } }
主活动:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" tools:context=".MainActivity"> </RelativeLayout>
detial java:
package com.example.administrator.myapplication932; import android.app.Activity; import android.os.Bundle; public class Detail extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); } }
detial活动:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/img" tools:context=".Detail"> </RelativeLayout>
没报错