【问题标题】:Send a notification when the app is closed应用关闭时发送通知
【发布时间】:2016-09-24 09:37:45
【问题描述】:

当应用程序完全关闭时,如何以编程方式发送通知?

示例:用户关闭了应用程序,也在 Android 任务管理器中,然后等待。应用应在 X 秒后或应用检查更新时发送通知。

我尝试使用这些代码示例,但是:

如果可以的话,试着用一个例子来解释它,因为初学者(比如我)可以更容易地学习它。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    您可以使用此服务,您只需在活动生命周期中启动此服务 onStop()。使用此代码: startService(new Intent(this, NotificationService.class)); 然后您可以创建一个新的 Java 类并将此代码粘贴到其中:

    public class NotificationService extends Service {
    
        Timer timer;
        TimerTask timerTask;
        String TAG = "Timers";
        int Your_X_SECS = 5;
    
    
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.e(TAG, "onStartCommand");
            super.onStartCommand(intent, flags, startId);
    
            startTimer();
    
            return START_STICKY;
        }
    
    
        @Override
        public void onCreate() {
            Log.e(TAG, "onCreate");
    
    
        }
    
        @Override
        public void onDestroy() {
            Log.e(TAG, "onDestroy");
            stoptimertask();
            super.onDestroy();
    
    
        }
    
        //we are going to use a handler to be able to run in our TimerTask
        final Handler handler = new Handler();
    
    
        public void startTimer() {
            //set a new Timer
            timer = new Timer();
    
            //initialize the TimerTask's job
            initializeTimerTask();
    
            //schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
            timer.schedule(timerTask, 5000, Your_X_SECS * 1000); //
            //timer.schedule(timerTask, 5000,1000); //
        }
    
        public void stoptimertask() {
            //stop the timer, if it's not already null
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    
        public void initializeTimerTask() {
    
            timerTask = new TimerTask() {
                public void run() {
    
                    //use a handler to run a toast that shows the current timestamp
                    handler.post(new Runnable() {
                        public void run() {
    
                            //TODO CALL NOTIFICATION FUNC
                            YOURNOTIFICATIONFUNCTION();
    
                        }
                    });
                }
            };
        }
    }
    

    之后你只需要将服务与manifest.xml结合起来:

    <service
                android:name=".NotificationService"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="your.app.domain.NotificationService" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </service>
    

    【讨论】:

    • 谢谢!我为我的应用测试了你的代码,它可以工作!
    • 根据this article,“TimerTask - 不在 UIThread 中运行,不可靠。共识是永远不要使用TimerTask。” Nikhil Gupta 的回答更好。
    【解决方案2】:

    您可以使用警报管理器来执行此操作。 请按照以下步骤操作:

    1) 使用alarmmanager 创建X 秒后的警报。

    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("NotificationText", "some text");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ledgerId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, 'X seconds in milliseconds', pendingIntent);
    

    2) 在您的应用中使用 AlarmBroadCast 接收器。

    在清单文件中声明:

    <receiver android:name=".utils.AlarmReceiver">
        <intent-filter>
            <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
    
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    

    3) 在广播接收器的接收中,您可以创建通知。

    public class AlarmReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // create notification here
        }
    }
    

    【讨论】:

    • 也感谢您的回答。我用你的代码试过了,它已经工作了。我决定使用来自@Vaibhav Kadam 的代码,因为它对我个人使用更具吸引力。
    • 这似乎是更受欢迎的方法。 guides.codepath.com/android/… 给出了更详细的描述。
    • 自 2021 年起,我认为这不再有效,应用程序关闭时警报管理器将不会运行
    【解决方案3】:

    您可以使用服务检查活动应用,如果活动未运行,则显示通知。

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 2022-06-20
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多