如何在应用关闭时获取Android通知?

12 notifications android android-notifications

从webserivce我得到日期和时间形式的数据,意味着某些特定日期我有一些插槽.下图给出了详细说明.在此输入图像描述

在每个日期,我都有一些时间安排.这里我想要的是在特定的日期和时间显示通知.如果数据库的响应包含明天日期,如2012年12月7日上午11:00.我需要在那时显示通知.

我对通知管理器和我正在使用的代码有一些想法..

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);
Run Code Online (Sandbox Code Playgroud)

但在这里我需要在应用程序关闭时收到通知.所以,任何人都可以帮助我.

Pet*_*fin 15

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

此外,您可以实现一个BroadcastReceiver,它将收听手机的Intents并在手机启动时启动您的服务!

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;
}
Run Code Online (Sandbox Code Playgroud)

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

//在清单标签中

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)

//在你的应用程序标签中

<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>
Run Code Online (Sandbox Code Playgroud)

如果您想从活动开始服务,请使用

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);
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*k R 5

如果数据来自您的服务器,则使用GCM可能是一种很好的方法.在这种情况下,服务器将能够唤醒/启动您的应用程序.

创建一个在您的情况下不断运行的服务是一个糟糕的解决方案.IMO更好的方法是使用AlarmManager.警报管理器将在特定时间调用意图.(请注意,如果手机重新启动,则必须再次注册意图).