相关疑难解决方法(0)

如何在Android中更新前台服务的通知文本?

我在Android中有一个前台服务设置.我想更新通知文本.我正在创建服务,如下所示.

如何更新在此前台服务中设置的通知文本?更新通知的最佳做法是什么?任何示例代码将不胜感激.

public class NotificationService extends Service {

    private static final int ONGOING_NOTIFICATION = 1;

    private Notification notification;

    @Override
    public void onCreate() {
        super.onCreate();

        this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, AbList.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);

        startForeground(ONGOING_NOTIFICATION, this.notification);

    }
Run Code Online (Sandbox Code Playgroud)

我正在我的主要活动中创建服务,如下所示:

    // Start Notification Service
    Intent serviceIntent = new Intent(this, NotificationService.class);
    startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)

service notifications android foreground

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

如何在不显示通知的情况下startForeground()?

我想创建一个服务并让它在前台运行.

大多数示例代码都有通知.但我不想透露任何通知.那可能吗?

你能举个例子吗?还有其他选择吗?

我的应用服务正在做媒体播放器.如何使系统不杀死我的服务,除了应用程序自杀(如按钮暂停或停止音乐).

android foreground android-service

72
推荐指数
9
解决办法
8万
查看次数

Android:如何避免点击通知调用onCreate()

在我的应用程序中,如果发生特殊情况,我会通知用户通知:

public void triggerNotification(String msg) {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent contentIntent = new Intent(this, ABC.class);
        Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
        notification.setLatestEventInfo(this, "ABC", msg, PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT));
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(notificationCounter, notification);
        notificationCounter++;
}
Run Code Online (Sandbox Code Playgroud)

如果用户单击Notification,则调用onCreate()方法.但我想要调用我的应用程序中的特定方法,或者如果应用程序不在前台,则将其带回前台.

我知道有很多教程可以解释如何处理通知,但我完全不理解它们,并且无法实现我想要的东西.

java user-interface notifications android

17
推荐指数
2
解决办法
8804
查看次数

Show ongoing notifications in Android Wear

I've been playing around with Android Wear and making some dummy apps. What I've noticed is that Android Wear doesn't seem to show the notifications that are ongoing,i.e., notifications with setOngoing(true). Any way to show these notifications on Wear???

android android-notifications wear-os

16
推荐指数
2
解决办法
3140
查看次数

前台服务示例

我是android的新手.任何人都可以发布"带有通知的android前台服务示例"的链接或代码.我用Google搜索,但没有得到前台服务的任何示例.

android android-intent android-layout android-fragments

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

由UI启动的第1部分持久foreGround android服务,也在睡眠模式下工作,也在手机重启时启动

Status:---同样接受Karakuri的和沙拉德Mhaske的答案,但由于沙拉德Mhaske回答之后赏金的开始,赏金应该去他.

第2 部分:第2 部分持续foreGround android服务,由UI启动,也在睡眠模式下工作,也从手机重启开始

stack overflow,只有一个答案可以接受.我认为这两个答案都是可以接受的,但必须选择一个(我随机选择).

观众邀请上/下投票答案/问题,以欣赏努力!.我赞成Karakuri的答案来弥补声誉.

Scenario:---

  1. 我想让用户单击开始/停止按钮并从UI活动启动/停止服务.我已经制作了用户界面,所以不关心它.但只是按钮点击事件的逻辑.

  2. 难道希望该服务绑定到用户界面活动.如果活动关闭,服务应继续运行.

  3. 想要尽最大努力保持服务的持久性并且在任何情况下都不会停止.将它赋予最大的权重并运行它,ForGroundSerice因为它具有更高的重要性层次.(希望没问题?)

  4. 除非我的应用程序用户界面点击了停止按钮,否则不要让它停止(或者应该重新启动)即使android回收内存.我和手机用户都知道了.这项服务至关重要.即使在睡觉.

    details =我的应用程序执行一些操作,为用户提供的时间(通常为15分钟)休眠,唤醒并再次执行操作.这永远不会结束)

    如果我需要AlarmManager,如何实现?或任何其他方式?或者只是把操作放在while loop and sleep for 15 minuts最后?

  5. 启动服务时(通过单击"开始"按钮).它应该输入一个条目,以便在手机重启时自动启动.

QUESTION:---

Primary Question:

  1. 只是无法为场景获得最佳策略 …

android android-intent android-service android-alarms android-backup-service

11
推荐指数
1
解决办法
5759
查看次数

在Android中叠加窗口服务

我必须像True Caller App一样实现Overlay Window.But我得到的问题是,在任何Incoming或Outgoing呼叫期间,我的服务会自动关闭或销毁.

服务类

public class OverlayService extends Service implements View.OnClickListener,NotifyHardwareChanges,UpdateSoundDB{

private WindowManager windowManager;
WindowManager.LayoutParams params;
View view;
Button btnEndCall;
public static TextView textView;
public static Context cntxt;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.
    return START_NOT_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    this.cntxt = getApplicationContext();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    params= new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE, …
Run Code Online (Sandbox Code Playgroud)

android popup android-service android-service-binding android-broadcastreceiver

11
推荐指数
1
解决办法
2029
查看次数

从最近的应用列表中删除应用时,请避免取消通知

我正在使用以下代码段显示来自我的应用内的服务的通知:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(currentNotificaion.getMessageTitle())
                .setContentIntent(contentIntent)
                .setContentText(currentNotificaion.getMessageText())
                .setAutoCancel(true);
int mNotificationId = (int) currentNotificaion.getMessageServerID();
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

我的服务在清单中声明如下:

<service
android:name="com.myapp.services.NotificationService"
android:stopWithTask="false">
</service>
Run Code Online (Sandbox Code Playgroud)

但是,当从最近的应用列表关闭我的应用时,通知将消失并从通知栏中删除.另一件事是我不会使用从通知栏中删除的粘贴通知.

我怎么能避免这个?

notifications android android-service

10
推荐指数
2
解决办法
605
查看次数

Android 通知,没有 NotificationCompat.Builder

我正在尝试创建通知,而不使用NotificationCompat.Builder. 如果不是真正需要(以及出于教育目的),动机不是使用支持库,我认为这是可能的,因为有“本机”NotificationNotificationManager向用户公开的类。

我的尝试如下:

Intent i = new Intent();
i.setClassName("com.test", "com.test.SomeActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);


notification.setLatestEventInfo(this, title, content,pi);

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

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

这段代码只是运行,但没有出现通知,并且notificationManager.notify()通常返回void.

android android-notifications

6
推荐指数
0
解决办法
775
查看次数

如何在android上创建一个永久的后台服务

我有一个噩梦试图在 android 上创建一个永久运行的简单后台服务。

此服务将执行一些后台任务,例如池用户社交媒体和显示通知,因此它只需要一次用户交互(登录)之后应该能够永远运行直到日子结束......

但它没有发生

这是我的清单:

<service
        android:name=".service.MyService"
        android:description="@string/serviceDescription"
        android:directBootAware="false"
        android:enabled="true"
        android:exported="false"
        android:icon="@drawable/ic_logo"
        android:label="@string/serviceLabel"/>
Run Code Online (Sandbox Code Playgroud)

我开始服务 Application onCreate()

    if (MyService.getInstance() == null) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }
Run Code Online (Sandbox Code Playgroud)

服务:

public void onCreate() {
        super.onCreate();
        this.workDone = 0;

        this.workerMap = new HashMap<User.UserData, Worker>(3);
        this.listeners = new HashMap<Worker, Worker.WorkerListener>(3);
        this.usernames = new HashMap<APIFacade, List<String>>(3);

        this.threadPool = Executors.newFixedThreadPool(3);
        INSTANCE = this;

    }
Run Code Online (Sandbox Code Playgroud)

在我的代码中绝对没有我调用 STOPSERVICE 或 STOPSELF

那么为什么在某些随机情况下我会得到 nullpointerexception 呢?

有人甚至告诉我执行以下代码以强制 android“重新启动”服务

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) { …
Run Code Online (Sandbox Code Playgroud)

android android-service android-background

6
推荐指数
1
解决办法
7853
查看次数

当电话进入空闲状态时,后台服务停止

我有一个粘性的后台服务,必须一直运行.基本上它跟踪时间,它是一个自定义时钟计时器.但是一旦设备进入空闲状态(当电话屏幕关闭时),定时器(后台服务)也会暂停.

即使屏幕关闭,我该怎么做才能使它始终保持运行?

    public class TimerService extends Service {
    private Context context;
    @Override
        public IBinder onBind(Intent intent) {
            Logger.LogI(TAG, "Service binding");
            return null;
        }

      @Override
        public void onCreate() {
            super.onCreate();
            context = getApplicationContext();
        }

     @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
       return Service.START_STICKY;
        }
    }
Run Code Online (Sandbox Code Playgroud)

performance service android

2
推荐指数
1
解决办法
5560
查看次数