Android Studio:在IntentFilter中需要帮助

时间:2021-12-02 02:01:51

I'm doing a small program in android studio and I need help in this code:

我正在android studio中做一个小程序,我在这段代码中需要帮助:

    Intent myIntent = new Intent(this , NotifyService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES, AlarmManager.INTERVAL_DAY, pendingIntent);
    BroadReceiver receiver = new BroadReceiver();
    IntentFilter filter = new IntentFilter("I NEED HELP HERE!");
    registerReceiver(receiver, filter);
    return super.onStartCommand(intent, flags, startId);

So, in the line I NEED HELP HERE, when I wrote Intent.ACTION_AIRPLANE_MODE_CHANGED it runs like it should be, but I need the code above to do the function of the ACTION_AIRPLANE_MODE_CHANGED. Basically, every fifteen minutes the background program (this code is there) should open a notification.

因此,在我需要帮助的行中,当我编写Intent.ACTION_AIRPLANE_MODE_CHANGED时,它运行应该是,但我需要上面的代码来执行ACTION_AIRPLANE_MODE_CHANGED的功能。基本上,每15分钟后台程序(此代码在那里)应该打开一个通知。

If you need more code just say.

如果您需要更多代码,请说。

1 个解决方案

#1


0  

First of all. AlramManager's PendingIntent contains Intent which will be fired when the alarm is fired. So, you should change:

首先。 AlramManager的PendingIntent包含将在触发警报时触发的Intent。所以,你应该改变:

Intent myIntent = new Intent(this , NotifyService.class);

to this

Intent myIntent = new Intent(this , BroadReceiver.class);

Here you can find more detailed examples Google's alarm guide

在这里,您可以找到更详细的Google报警指南示例

If you want to use implicit intent, then you can try like this:

如果你想使用隐式意图,那么你可以尝试这样:

Intent myIntent = new Intent();
myIntent.setAction("your_action");
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

// setup AlarmManager

BroadReceiver receiver = new BroadReceiver();
IntentFilter filter = new IntentFilter("your_action");
registerReceiver(receiver, filter);

#1


0  

First of all. AlramManager's PendingIntent contains Intent which will be fired when the alarm is fired. So, you should change:

首先。 AlramManager的PendingIntent包含将在触发警报时触发的Intent。所以,你应该改变:

Intent myIntent = new Intent(this , NotifyService.class);

to this

Intent myIntent = new Intent(this , BroadReceiver.class);

Here you can find more detailed examples Google's alarm guide

在这里,您可以找到更详细的Google报警指南示例

If you want to use implicit intent, then you can try like this:

如果你想使用隐式意图,那么你可以尝试这样:

Intent myIntent = new Intent();
myIntent.setAction("your_action");
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

// setup AlarmManager

BroadReceiver receiver = new BroadReceiver();
IntentFilter filter = new IntentFilter("your_action");
registerReceiver(receiver, filter);