首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在服务中使用自定义通知?

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

Stack Overflow用户
提问于 2018-07-19 18:51:07
回答 1查看 155关注 0票数 0

我有一个服务,我尝试在服务中使用customview (remoteview)来构建通知。我必须在通知中单击3图像。

我的自定义布局只有3个图像视图,我在服务中启动了onclickevent,但我没有

我尝试了许多解决方案,但都不能成功

请帮帮我:-D

我的清单:

代码语言:javascript
复制
<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>

我的服务类:

代码语言:javascript
复制
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);
}

和我的自定义布局:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2018-07-29 01:33:59

在你的getPendingSelfIntent()中,getClass() -是Ogame1.class,但是要触发你的接收器,你需要notify_receiver.class。另外,你不需要清单中的动作,因为你的待定意图是显式的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51420765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档