安卓8.0及以上使用通知
阿新 • • 發佈:2019-03-20
inflate not in amp open pan uniq rar extend activity
然後發送通知:
代碼如下:
package shurui.loyal888.com.myapplication; import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.Build; import android.os.Bundle; import android.os.PersistableBundle; import android.support.v4.app.NotificationCompat; importView Codeandroid.support.v4.app.NotificationManagerCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { private static finalString TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createNotificationChannel(); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1") .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My notification") .setContentText("Much longer text that cannot fit one line...") .setStyle(new NotificationCompat.BigTextStyle() .bigText("Much longer text that cannot fit one line...")) .setPriority(NotificationCompat.PRIORITY_DEFAULT); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); // notificationId is a unique int for each notification that you must define notificationManager.notify(1, builder.build()); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true; } public void onGroupItemClick(MenuItem item) {} @Override protected void onRestart() { Log.i(TAG, "onRestart: "); super.onRestart(); } /** * android 8.0 以上通知 必須先獲取chanle */ private void createNotificationChannel() { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = getString(R.string.appbar_scrolling_view_behavior); String description = getString(R.string.appbar_scrolling_view_behavior); int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel channel = new NotificationChannel("1", name, importance); channel.setDescription(description); // Register the channel with the system; you can‘t change the importance // or other notification behaviors after this NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } }
安卓8.0及以上使用通知