如何获得Android的通知时,应用程序被关闭? [英] How to get android notifications when app was closed?
问题描述
从webserivce我得到的数据的日期和时间的形式,手段我有一些组时隙一些特定的日期。下图给出了详细的解释。
From webserivce I am getting data in the form of dates and time, means for some particular date i am having some set of slots. The following diagram gives the detailed explanation.
下面的每个日子,我有一些设置为定时。在这里,我想是在特定的日期和时间显示的通知。如果从数据库中响应由明天日期像2012年7月12日上午11:00。我需要在那个时候,显示通知。
Here on each date I have some set to timings. Here what i want is to display notification at the particular date and time. If the response from database consists of tomorrow date like 07/12/2012 at 11:00 AM. I need to display notification at that time.
我有一个关于通知管理器和$ C $词现在用的就是一些想法..
I have some idea regarding notification manager and code i am using is..
Main.java
Main.java
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_action_search, "A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(Main.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
但是在这里我需要得到通知时,应用程序也将关闭。那么,谁能帮我这一点。
But here i need to get notifications when app is closed also. So, anyone help me with this.
推荐答案
添加(启动)服务
到您的应用程序。即使用户辞去您的应用程序,该服务仍然会在后台运行。
Add a (started) Service
to your application. Even though the user quitted your app, the service will still run in the background.
此外,你还可以实现一个BroadcastReceiver会听电话的意图,让您的服务启动时,手机的靴子!
Furthermore you can implement a BroadcastReceiver that will listen to the phone's Intents and let your service be started when the phone boots !
MyService.java
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId){
// START YOUR TASKS
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// STOP YOUR TASKS
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent){
return null;
}
BootReceiver.java
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("your.package.MyService");
context.startService(serviceIntent);
}
}
}
}
AndroidManifest.xml中
//在明显的标签
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
//在你的标签
// in your application tag
<service android:name=".MyService">
<intent-filter>
<action android:name="your.package.MyService" />
</intent-filter>
</service>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
如果您想从活动启动您的服务,只要使用
If you want to start your service from an activity, just use
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
if (!isMyServiceRunning()){
Intent serviceIntent = new Intent("your.package.MyService");
context.startService(serviceIntent);
}
这篇关于如何获得Android的通知时,应用程序被关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!