相关疑难解决方法(0)

在自定义通知中添加按钮操作

我已经制作custom notification并且有一个按钮,我想要执行两个不同的functionalities on notification and button click.我看了很多链接但找不到添加按钮监听器的方法.

谁能帮忙.这是我的代码.非常感谢.

 private void startNotification() {
    Intent intent;
    PendingIntent pIntent;
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.mynotification);

    Context context = getApplicationContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher).setContent(
            remoteViews);

    if (hasFlash) {
        intent = new Intent(context, FlashLight.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    } else {
        intent = new Intent(context, BlankWhiteActivity.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    }
    builder.setContentIntent(pIntent);
    NotificationManager mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notif = builder.setContentTitle("Flashlight")
            .setContentText("Lighten your world!!!").build();
    mNotificationManager.notify(1, …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-layout android-notifications android-fragments

31
推荐指数
1
解决办法
5万
查看次数

没有GUI的Android Activity

我创建了一个仅用于从链接启动的活动(使用intent过滤器.)我不希望这个活动有一个GUI - 我只是想让它启动一个服务并在栏中发出通知.我试图在我的服务中为链接设置intent过滤器,但这不起作用.有没有更好的方法来回答意图过滤器 - 或者我可以让我的活动没有GUI吗?
对不起,如果我困惑,艾萨克

user-interface android intentfilter broadcastreceiver android-service

29
推荐指数
2
解决办法
3万
查看次数

事件OnClick用于自定义通知中的按钮

我有一个带按钮的自定义通知.要设置通知并使用事件OnClick on my button,我使用了以下代码:

//Notification and intent of the notification 
Notification notification = new Notification(R.drawable.stat_notify_missed_call,
            "Custom Notification", System.currentTimeMillis());

Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),
    0, mainIntent , 0);
notification.contentIntent = pendingMainIntent;

//Remoteview and intent for my button
RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),
    R.layout.remote_view_layout);

Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));
PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,
            activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.button1,
    pendingLaunchIntent);

notification.contentView = notificationView;

notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)

使用此代码,我使用自定义布局自定义通知...但我无法单击按钮!每次我尝试单击按钮时,我都会单击整个通知,因此脚本会启动"mainIntent"而不是"activityIntent".

我在互联网上看到这段代码不适用于所有终端.我已经在模拟器和HTC Magic上尝试过但我总是遇到同样的问题:我无法点击按钮!

我的代码是对的?有人可以帮帮我吗?

谢谢,

西蒙娜

notifications android onclick button

27
推荐指数
2
解决办法
4万
查看次数

START_STICKY,前台Android服务在没有通知的情况下消失

我在我的新应用程序中启动了一项服务.该服务是有前途的,带有通知.当在AVD 2.1 API Level 7中运行时,一切正常.但是当它在运行Gingerbread的三星Galaxy Tab上运行时,服务将启动(图标和应用程序名称出现在通知区域的顶部),但几秒钟后,服务就会消失.我可以看到的Log中的最后一个条目与我的App相关联,是我的Log.d("Taglines","Return with with"+ START_STICKY)的结果,它紧接在"return START_STICKY"之前.在我的服务的onStartCommand覆盖中,如下所示:

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

    int rc ;
    Log.d("Taglines","onStartCommand()");
    Toast.makeText(this, "Starting service TagsManager", Toast.LENGTH_SHORT).show();
    Log.d("Taglines","Calling super.onStartCommand()");
    rc = super.onStartCommand(intent,flags,startId);
    Log.d("Taglines","super.onStartCommand return code was " + rc);
    createNotification(INITIAL_NOTIFICATION_TEXT);
    Log.d("Taglines","Returning with " + START_STICKY);
    return START_STICKY ;
}
Run Code Online (Sandbox Code Playgroud)

通知设置如下:

void createNotification(String text) {

    Log.d("Taglines","createNotification called");
    if (mNotificationManager == null) {
        // Get a reference to the Notification Manager
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);
        Log.d("Taglines","Obtained …
Run Code Online (Sandbox Code Playgroud)

service android foreground

7
推荐指数
1
解决办法
1万
查看次数

按钮执行操作单击自定义通知:Android

我正在尝试执行一些动作,如暂停音乐,在按钮点击Android中的自定义通知播放音乐.目前我这样做,

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, "Custom Notification", when);

    NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout);
    contentView.setTextViewText(R.id.textView1, "Custom notification");
    contentView.setOnClickPendingIntent(R.id.button1, pIntent);
    notification.contentView = contentView;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentIntent = contentIntent;

    notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
    notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound

    mNotificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)

但是这个带我去另一个活动.无论如何都要对同一活动实施通知操作.

例如..让我说我提出了一个通知,当用户按下它时,然后在我当前的活动/服务中调用一些常规方法而不是带我去做某些活动

android onclick android-notifications

4
推荐指数
1
解决办法
1万
查看次数