只要应用程序在Android中打开,就会发送通知

时间:2015-03-13 09:02:48

标签: android notifications alarmmanager

我有一个应用程序,每天在确切的时间发送notification AlarmManager

但它有一个错误。 只要应用程序处于打开状态,就会发送通知

如何每天发送一次通知?谢谢

MAINACTIVITY

public class MainActivity extends Activity {

private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();

    int d = Integer.valueOf(1440);
    calendar.setTimeInMillis(System.currentTimeMillis());

    Intent i = new Intent(MainActivity.this, Receiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, i, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * d, pendingIntent);
}
}

RECEIVER

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent i = new Intent(context, MainActivity.class);

    Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());

    long[] vibrate = {100, 100, 200, 300};
    notification.vibrate = vibrate;
    notification.defaults = Notification.DEFAULT_ALL;

    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 0, i,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

    notificationManager.notify(0, notification);
}
}

1 个答案:

答案 0 :(得分:0)

  

每隔int d分钟调用CallThisClass.class的代码

Intent myIntent = new Intent(getActivity(), CallThisClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
int d = Integer.valueOf(duration);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * d, pendingIntent);
  

停止调用CallThisClass.class的代码

Intent myIntent = new Intent(getActivity(), CallThisClass.class);
PendingIntent  pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
  

CallThisClass.class

public class CallThisClass extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.i("Received", "Broadcast Received !");
        // Do your stuff
    }
}
  

不要忘记你的AndroidManifest.xml中的Receiver

<receiver android:name=".CallThisClass" />
  

和许可

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

您可以随时开始调用CallThisClass.class。例如。点击按钮,或主要活动开始时。当你不想让它工作时,请确保停止调用该类。例如,当用户第一次安装你想要的应用程序时,你想要每24小时更新一次(通知或其他东西),rit?在你的prefrences中存储一个变量,该变量只有在第一次加载时才会自行更改。每次用户输入应用程序时检查该变量。如果是第一次启动呼叫代码。 (现在我们不想调用停止代码,因为即使应用程序关闭,我们也不能继续调用该类!)关闭应用程序,它将工作。你可以在你的设置中有一个按钮或者用于测试的东西,当点击它时会运行代码而不调用类。它将每隔d分钟停止呼叫呼叫类,即你的情况下24小时(也可以在此按钮的clik上将prefrnce更改为默认值,以便下次启动活动或单击激活或再次启动该代码以启动呼叫时上课开始)。我希望它有所帮助。

  

另外,我建议阅读here for Best practices