8.0的适配

8.0的图标适配:Ctrl Shift A 弹出框  输入image Assert

分为前景图和背景图(可以是背景颜色或者是png、jpg的图片)

8.0的通知栏适配:

package huike.qu.com.testicon;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 创建渠道
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            String chatChannelId = "chat";
            String chatChannelName = "聊天";
            int chatImportance = NotificationManager.IMPORTANCE_HIGH;
            createNoticficationChannel(chatChannelId,chatChannelName,chatImportance);

            String SubscribChannelId = "subscribe";
            String SubscribChannelName = "订阅";
            int SubscribImportance = NotificationManager.IMPORTANCE_DEFAULT;
            createNoticficationChannel(SubscribChannelId,SubscribChannelName,SubscribImportance);
        }

    }
    @TargetApi(Build.VERSION_CODES.O)
    public void createNoticficationChannel(String channelId,String channelName,int importance){
         NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
         channel.setShowBadge(true);//是否显示角标未读数
         manager.createNotificationChannel(channel);
    }
    //发送聊天消息
    public void sendChatMsg(View v){
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //判断当前渠道是否被关闭
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = manager.getNotificationChannel("chat");
            if(channel.getImportance() == NotificationManager.IMPORTANCE_NONE){
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE,getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID,channel.getId());
                startActivity(intent);
                Toast.makeText(this,"请手动打开通知",Toast.LENGTH_SHORT).show();
            }
        }

        Notification notification = new NotificationCompat.Builder(this,"chat")
                .setContentTitle("受到了一条聊天消息")
                .setContentText("你今天中午吃饭了吗?")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
                .setNumber(15) //设置角标未读数
                .setAutoCancel(true)
                .build();
        manager.notify(1,notification);
    }

    //发送订阅消息
    public void sendSubscribeMsg(View v){
       NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       Notification notification = new NotificationCompat.Builder(this,"subscribe")
               .setContentTitle("接收到了一条订阅消息")
               .setContentText("武汉长江大桥将在9月份开通")
               .setWhen(System.currentTimeMillis())
               .setSmallIcon(R.drawable.ic_launcher_foreground)
               .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
               .setNumber(10)
               .setAutoCancel(true)
               .build();
        manager.notify(2,notification);
    }
}

猜你喜欢

转载自my.oschina.net/quguangle/blog/1819858