如何在服务中使用自定义通知?

时间:2018-07-19 10:51:07

标签: android service notifications remoteview

我有一项服务,我尝试在服务中使用customview(remoteview)构建通知。 我必须在通知中点击3张图片。

我的自定义布局仅具有3个imageview 我在服务中启动了onclickevent,但是我没有

我尝试了许多解决方案,但我没有成功

请帮助我:-D

我的清单:

<service android:name=".services.Service1"></service>
    <receiver android:name=".adapters.AlarmReceiver" />
    <receiver android:name=".services.SSSReceiver" android:enabled="true" android:process=":remote" />
    <receiver android:name=".services.Service1$notify_receiver" android:exported="false">
        <intent-filter>
            <action android:name="cn_wv" />
            <action android:name="cn_exit" />
            <action android:name="cn_logo" />
        </intent-filter>
    </receiver>

我的服务等级:

public class Ogame1 extends Service {

Context ctx;

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

public class notify_receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("notify_receiver","onReceive");
    }
}

@Override
public void onCreate() {
    ctx = getApplicationContext();
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.ly_customnotify);
    contentView.setOnClickPendingIntent(R.id.cn_wv, getPendingSelfIntent(ctx, "cn_wv"));
    contentView.setOnClickPendingIntent(R.id.cn_exit, getPendingSelfIntent(ctx, "cn_exit"));
    contentView.setOnClickPendingIntent(R.id.customnotify_logo, getPendingSelfIntent(ctx, "cn_logo"));



    Notification myNotify  = new Notification.Builder(ctx)
            .setSmallIcon(R.drawable.logo)
            .setContent(contentView)
            .setOngoing(true)
            .setPriority(Notification.PRIORITY_MAX).build();
    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, myNotify);
}

和我的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp">

<ImageView
    android:id="@+id/customnotify_logo"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:layout_weight=".60"
    android:contentDescription="@string/app_name"
    android:scaleType="fitCenter"
    android:src="@drawable/logo" />

<ImageView
    android:id="@+id/cn_wv"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:layout_weight=".20"
    android:contentDescription="@string/app_name"
    android:scaleType="fitCenter"
    android:src="@android:drawable/button_onoff_indicator_on" />
<ImageView

    android:id="@+id/cn_exit"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:layout_weight=".20"
    android:contentDescription="@string/app_name"
    android:scaleType="fitCenter"
    android:src="@android:drawable/ic_lock_power_off" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在您的getPendingSelfIntent()中,getClass()-是Ogame1.class,但是要触发您的接收器,您需要notify_receiver.class。同样,您不需要清单中的操作,因为您的待定意图是明确的。