Android 当应用程序关闭时发送通知

Android 当应用程序关闭时发送通知,android,notifications,Android,Notifications,当应用程序完全关闭时,如何以编程方式发送通知 示例:用户关闭应用程序,也在Android Taskmanager中,然后等待。应用程序应在X秒后或应用程序检查更新时发送通知 我尝试使用这些代码示例,但: -活动太多/不起作用 -很多信息,但我不知道如何处理 -很多信息,但我不知道如何处理 如果可以,试着用一个例子来解释它,因为初学者(像我一样)可以通过这种方式更容易地学习。您可以使用alarm manager来完成这项工作。 请遵循以下步骤: 1) 使用alarmmanager在X秒后创建报

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

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

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

  • -活动太多/不起作用
  • -很多信息,但我不知道如何处理
  • -很多信息,但我不知道如何处理

如果可以,试着用一个例子来解释它,因为初学者(像我一样)可以通过这种方式更容易地学习。

您可以使用alarm manager来完成这项工作。 请遵循以下步骤:

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>

您可以使用服务检查活动应用程序,并在活动未运行时显示通知。

您可以使用此服务,您只需在活动生命周期的顶部()启动此服务即可。使用此代码:
startService(新意图(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>


谢谢!我为我的应用程序测试了你的代码,它运行正常!根据Nikhil Gupta的回答,“TimerTask-不在UIThread中运行并且不可靠。一致意见是永远不要使用。”Nikhil Gupta的回答更好。也感谢您的回答。我用你的代码试过了,已经可以用了。我决定使用@Vaibhav-Kadam的代码,因为它对我个人来说更具吸引力。这似乎是我更喜欢的方法。给出了更详细的描述。
<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>