点按以获取更多信息或停止应用

时间:2017-01-16 17:32:23

标签: android multithreading service notifications intentservice

我创建服务和自定义通知任何工作正常,但android说 应用程序正在通知运行 点按以获取更多信息或停止该应用。 我该如何解决这个问题。

我的自定义BroadcastReceiver

public class ServiceBootCompleteReciver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, ServiceNotification.class);
    context.startService(service);

  }

我需要取消注册此接收器吗?

我的自定义硝化类

    public class CustomNotification extends Notification {
      public void notifiaction(String txt) {
        ThreadSms thread = new Thread();
        threadSms.start();
;
        if (ThreadSms.newMessage) {
          Intent notificationIntent = new Intent(GlobalController.currentActivity, ActivityMain.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(GlobalController.context, 0, notificationIntent, 0);
          Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          RemoteViews remoteViews = new RemoteViews(GlobalController.context.getPackageName(), R.layout.custom_toast);
          remoteViews.setTextViewText(R.id.text, txt);
          NotificationCompat.Builder builder = new NotificationCompat.Builder(GlobalController.context)
            .setSmallIcon(R.drawable.small_icon)
            .setContent(remoteViews)
            .setContentIntent(pendingIntent)
            .setSound(defaultSoundUri).setAutoCancel(false)
            .setLargeIcon(BitmapFactory.decodeResource(GlobalController.context.getResources(), R.drawable.modem))
            .setColor(Color.argb(255, 255, 152, 0))
            .setPriority(Notification.PRIORITY_MAX);
          GlobalController.notificationManager.notify(0, builder.build());

        }
      }
    }

我的自定义服务

public class ServiceNotification extends IntentService {

  public ServiceNotification() {
    super("notify");
  }

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

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

      GlobalController.timerTaskService = new TimerTask() {
        @Override
        public void run() {
          CustomNotification customNotification = new CustomNotification();
          customNotification.notifiaction(message);
          startForeground(1, customNotification);
        }
      };

      GlobalController.timerService.scheduleAtFixedRate(GlobalController.timerTaskService, 0, 5000);

    return Service.START_STICKY;
  }

  public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(),
      this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI);

  }

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          android:versionCode="1"
          android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="25"/>

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:name=".app.GlobalController"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        <activity android:name=".activity.ActivityLogin">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".activity.ActivitySplashScreen"/>
        <activity android:name=".activity.ActivityMain"/>
        <activity android:name=".activity.ActivityAdvanceSetting"/>
        <receiver
            android:name=".services.ServiceBootCompleteReciver"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <service
            android:name=".services.ServiceNotification"
            android:enabled="true"
            android:exported="false"/>
    </application>

1 个答案:

答案 0 :(得分:0)

在Android 10上,您必须在通知上调用setSmallIcon ,否则该通知将被忽略。 (在Android 11上并非如此,无论您是否设置了小图标,它都可以正常工作。)如果您未能设置小图标,则您将看到罐装Android通知,而不是自己的通知:

enter image description here

此带有setSmallIcon的代码可以正常工作:

                new Notification.Builder(getApplicationContext(), channelId)
                        .setSmallIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setLargeIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setContentTitle(title)
                        .setContentText(text)
                        .setCategory(Notification.CATEGORY_CALL)
                        .setExtras(extras)
                        .setAutoCancel(true)
                        .addAction(android.R.drawable.ic_menu_call, "Accept", piAcceptIntent)
                        .setFullScreenIntent(pendingIntent, true);

此不含setSmallIcon的代码会导致问题:

                new Notification.Builder(getApplicationContext(), channelId)
                        .setLargeIcon(Icon.createWithResource(this, R.drawable.myicon))
                        .setContentTitle(title)
                        .setContentText(text)
                        .setCategory(Notification.CATEGORY_CALL)
                        .setExtras(extras)
                        .setAutoCancel(true)
                        .addAction(android.R.drawable.ic_menu_call, "Accept", piAcceptIntent)
                        .setFullScreenIntent(pendingIntent, true);

在上面的评论中,@ royas和@Flamychandayo找到了此解决方案。我将其添加为答案,以使遇到此问题的人员更清楚地了解该解决方案。