Exc*_*el1 10 notifications android
当应用程序完全关闭时,如何以编程方式发送通知?
示例:用户也在Android Taskmanager中关闭了应用程序,然后等待.应用程序应在X秒后或应用程序检查更新时发送通知.
我尝试使用这些代码示例但是:
如果可以,尝试在一个例子中解释它,因为初学者(像我一样)可以更容易地学习它.
小智 9
您可以使用警报管理器来执行此操作。请按照以下步骤操作:
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);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
3) 在广播接收器的接收中,您可以创建通知。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// create notification here
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此服务,只需在活动生命周期中在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();
}
});
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
之后,您只需要将服务与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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18870 次 |
| 最近记录: |