android自定义通知问题

时间:2016-10-18 11:13:41

标签: android customization android-notifications

Here i am able to get this type of notification but my application stops in below 5.0. So, what can i do to have this kind of notification manager. It seems to be an error for an small image loading before the notification text

这是我的代码

public static PendingIntent getDismissIntent(int notificationId,
            Context context) {
        Intent intent = new Intent(context, ClsNotificationCloseReceiver.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return dismissIntent;
    }

    private void startNotification() {

        String ns = Context.NOTIFICATION_SERVICE;

        notificationManager = (NotificationManager) MainActivity.this
                .getSystemService(NOTIFICATION_SERVICE);

        PendingIntent dismissIntent = MainActivity.getDismissIntent(1,
                MainActivity.this);

        @SuppressWarnings("deprecation")
        Notification notification = new Notification(
                R.drawable.notification_icon, "abc", System.currentTimeMillis());

        RemoteViews notificationView = new RemoteViews(getPackageName(),
                R.layout.mynotification);

        // the intent that is started when the notification is clicked (works)
        Intent intent = new Intent(MainActivity.this, MainActivity.class);

        // PendingIntent pendingIntent = PendingIntent
        // .getActivity(MainActivity.this, 1, intent,
        // PendingIntent.FLAG_UPDATE_CURRENT);

        notification.contentView = notificationView;
        // notification.contentIntent = pendingIntent;
        notification.flags |= Notification.FLAG_NO_CLEAR
                | Notification.FLAG_ONGOING_EVENT;

        // this is the intent that is supposed to be called when the
        // button is clicked
        Intent switchIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
                switchIntent, 0);

        notificationView.setOnClickPendingIntent(R.id.closeOnFlash,
                dismissIntent);
        notificationManager.notify(1, notification);
    }

mynotification.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="100" >

    <ImageView
        android:id="@+id/notifiation_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="38"
        android:contentDescription=" vvxcv"
        android:paddingLeft="8dp"
        android:src="@drawable/notification_icon" />

    <TextView
        android:id="@+id/appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/notifiation_image"
        android:layout_toRightOf="@+id/notifiation_image"
        android:layout_weight="57"
        android:paddingLeft="8dp"
        android:text="@string/app_name"
        android:textColor="#000000"
        android:textSize="15dp" />

    <Button
        android:id="@+id/closeOnFlash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:background="@drawable/btnstop"
        android:onClick="stopRecording" />

</LinearLayout>

我正在调用startNotification方法,但它会停止。我想要这个东西:https://drive.google.com/file/d/0BwgJuKXMh_yGdG1YX1BJUUVzSTA/view?usp=sharing并且在按下停止按钮

之前通知不应该被清除

1 个答案:

答案 0 :(得分:0)

如果出现错误,请发送Logs First, 我认为可以使用NotificationCompat.Builder类和&amp;然后在运行之前清理项目。

new NotificationCompat.Builder(getApplicationContext());