应用程序关闭时如何获取android通知? [英] How to get android notifications when app was closed?

查看:33
本文介绍了应用程序关闭时如何获取android通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从网络服务中,我以日期和时间的形式获取数据,这意味着对于某个特定日期,我有一组插槽.下图给出了详细的解释.

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.

在这里,我在每个日期都有一些时间安排.这里我想要的是在特定日期和时间显示通知.如果来自数据库的响应包含明天的日期,例如 07/12/2012 上午 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.

我对通知管理器有一些想法,我使用的代码是..

I have some idea regarding notification manager and code i am using is..

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.

推荐答案

向您的应用程序添加(已启动)Service.即使用户退出了您的应用程序,该服务仍将在后台运行.

Add a (started) Service to your application. Even though the user quitted your app, the service will still run in the background.

此外,您还可以实现一个 BroadcastReceiver,它会监听手机的 Intent,并在手机启动时启动您的服务!

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆