如何在android中制作自定义通知大图标

时间:2015-07-26 14:10:22

标签: android

我打算制作自定义通知栏。但不是很好。 我想创建一个大图标。

      remoteViews = new RemoteViews(getPackageName(), R.layout.widget_main);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Title")
                .setTicker("Ticker")
                .setSmallIcon(R.mipmap.howlong)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.howlong))//I dot understand why entering this icon.
                .setContent(remoteViews).setOngoing(true);

这是我的代码。

https://www.dropbox.com/s/njywqp5s5swtfdd/img.jpg?dl=0

这是我的通知状态。

如何放置大图标?

------------------------------------添加代码--------- ----------------------

package com.example.namsoo.mynotificationpractice5;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import android.widget.Toast;


/**
 * Created by namsoo on 2015-07-22.
 */

    /*
    Add code for manifest

    <service android:name=".WidgetService"
    android:enabled="true"
    android:exported="true" >
    </service>
    */


    /*
    Add code for MainActivity.onCreate

    Intent widgetIntent = new Intent(this, WidgetService.class);
    startService(widgetIntent);
    */

public class WidgetService extends Service {

    RemoteViews remoteViews;
    NotificationManager notificationManager;

    int NOTIFICATION_ID = 111;
    int INITIAL_VALUE = 0;//초기값
    int FUNCTION_ON =1;//ON
    int FUNCTION_OFF =2;//OFF
    int functionFlag = 0;//0 = 초기 1 = ON 2 = OFF



    @Override
    public void onCreate() {
        Toast.makeText(getApplicationContext(), "Start Widget Service", Toast.LENGTH_LONG).show();

        startWidget();

        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        remoteViews = new RemoteViews(getPackageName(), R.layout.widget_main);

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

        String btn = intent.getStringExtra("btn");

        if (btn != null)
        {
            if (btn.equals("state"))
            {
                //function code or Value redirects.
                startWidget();
                Toast.makeText(getApplicationContext(), "btn : " + btn + startId + " " + flags, Toast.LENGTH_LONG).show();
            }
            else if (btn.equals("exit"))
            {
                Toast.makeText(getApplicationContext(), "btn : " + btn + startId + " " + flags, Toast.LENGTH_LONG).show();
                notificationManager.cancelAll();
                stopService(intent);
                System.exit(1);
            }
            else
            {
                Toast.makeText(getApplicationContext(), "not null btn : " + btn, Toast.LENGTH_LONG).show();
            }

        }
        else
        {
            Toast.makeText(getApplicationContext(), " null btn", Toast.LENGTH_LONG).show();
        }

        return super.onStartCommand(intent, flags, startId);
    }//서비스 실행


    public void startWidget() {
        if (functionFlag != INITIAL_VALUE) {
            remoteViews.removeAllViews(R.id.widgetLayout);
        }//노피티케이션 뷰 삭제(처음이 아닐때만 실행)

        remoteViews = new RemoteViews(getPackageName(), R.layout.widget_main);//뷰 추가

        setWidgetButtonListeners();//버튼이벤트 추가

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(remoteViews).setOngoing(true);


        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.addFlags(resultIntent.FLAG_ACTIVITY_NEW_TASK | resultIntent.FLAG_ACTIVITY_SINGLE_TOP);//화면이 없는 상태에서 만들어주는 플래그 || 화면이 있으면 화면을 재사용하세요.


        PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, 0);

        // start the activity when the user clicks the notification text
        mBuilder.setContentIntent(resultPendingIntent).setAutoCancel(false);

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

        // pass the Notification object to the system
        notificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }



    public void setWidgetButtonListeners() {

        if (functionFlag == INITIAL_VALUE) {
            remoteViews.setImageViewResource(R.id.btnState, android.R.drawable.ic_media_pause);
            remoteViews.setTextViewText(R.id.tv, "On~~");
            functionFlag = FUNCTION_ON;
        } else if (functionFlag == FUNCTION_ON)//On 일 때
        {
            remoteViews.setImageViewResource(R.id.btnState, android.R.drawable.ic_media_play);
            remoteViews.setTextViewText(R.id.tv, "Off~~");
            functionFlag = FUNCTION_OFF;
        } else if (functionFlag == FUNCTION_OFF)//Off 일 때
        {
            remoteViews.setImageViewResource(R.id.btnState, android.R.drawable.ic_media_pause);
            remoteViews.setTextViewText(R.id.tv, "On");
            functionFlag = FUNCTION_ON;
        }//버튼과 텍스트의 값 변경

        Intent intentBtnState = new Intent(getApplicationContext(), this.getClass());
        intentBtnState.putExtra("btn", "state");
        PendingIntent piState = PendingIntent.getService(getApplicationContext(), R.id.btnState, intentBtnState, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btnState, piState);

        Intent intentBtnExit = new Intent(getApplicationContext(), this.getClass());
        intentBtnExit.putExtra("btn", "exit");
        PendingIntent piExit = PendingIntent.getService(getApplicationContext(), R.id.btnExit, intentBtnExit, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btnExit, piExit);
        //이벤트 추가
    }


    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(), "Destroy", Toast.LENGTH_LONG).show();

        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

private Bitmap getCircleBitmap(Bitmap bitmap) {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(output);

        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        bitmap.recycle();

        return output;
    }