如何设置调度的通知中的android [英] How to set Scheduled notification in android

查看:124
本文介绍了如何设置调度的通知中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序,即使应用程序没有运行10秒后抛出一个通知。我已经使用了 AlarmManager ,但无法得到通知在期望的时间。 我能得到一个敬酒那个时候,但没有通知。任何帮助会深深AP preciated。

这是我AlarmManager类:

 公共类AlarmManagerActivity延伸活动{

私人AlarmManagerBroadcastReceiver报警;
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_alarm_manager);
    报警=新AlarmManagerBroadcastReceiver();
    onetimeTimer();


}

@覆盖
保护无效的OnStart(){
    super.onStart();
}


公共无效notifyN(){

    意向意图=新的意图(这一点,NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(此,0,意图,0);

    //构建通知
    //操作都只是假的
    通知NotI位=新Notification.Builder(本)
            .setContentTitle(这是一个简单的通知)
            .setContentText(主题)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_action_search,呼叫,pIntent)
            .addAction(R.drawable.ic_action_search,更多,pIntent)
            .addAction(R.drawable.ic_action_search,多,pIntent).build();


    NotificationManager notificationManager =
      (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    //隐藏其选中后通知
    noti.flags | = Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0,NotI位);
}


公共无效onetimeTimer(){
    上下文的背景下= this.getApplicationContext();
    如果(报警!= NULL){
        alarm.setOnetimeTimer(上下文);
        notifyN();

    }其他{
        Toast.makeText(背景下,报警为空,Toast.LENGTH_SHORT).show();
    }
}

}
 

这是AlarmManagerBroadcastReceiver:

 公共类AlarmManagerBroadcastReceiver扩展的BroadcastReceiver {

最终公共静态字符串ONE_TIME =一次性;

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    电源管理器PM =(电源管理器)上下文
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock WL = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK,您的标记);
    //获取锁
    wl.acquire();

    //你可以做处理此更新的窗口小部件/远程视图。
    捆绑额外= intent.getExtras();
    StringBuilder的msgStr =新的StringBuilder();

    如果(临时演员= NULL和放大器;!&安培; extras.getBoolean(ONE_TIME,Boolean.FALSE)){
        msgStr.append(有一次定时器:);
    }
    格式格式化=新的SimpleDateFormat(HH:MM:SS);
    msgStr.append(ABCS停止实例+ formatter.format(新日期()));

    Toast.makeText(上下文,msgStr,Toast.LENGTH_LONG).show();

    //释放锁
    wl.release();

}

公共无效setOnetimeTimer(上下文的背景下){
    AlarmManager AM =(AlarmManager)上下文
            .getSystemService(Context.ALARM_SERVICE);
    意向意图=新的意图(背景下,AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME,Boolean.TRUE);
    PendingIntent圆周率= PendingIntent.getBroadcast(上下文,0,意图,0);
    am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ 10000,PI);

}
}
 

解决方案

创建一个类(称之为像 ScheduledService ),并让它延长 IntentService 。在这个类,你会做你想做的事时,警报响起。

 公共类ScheduledService扩展IntentService {

公共ScheduledService(){
    超(我的服务);
}

@覆盖
保护无效onHandleIntent(意向意图){
    //做一些事情,触发一个通知或任何你想在这里做
    Log.d(调试,环环!);

}
}
 

那么,你的活动启动警报使用以下命令:

  AlarmManager经理=(AlarmManager)YourActivity.getSystemService(Context.ALARM_SERVICE);
意图I =新的意图(YourActivity,ScheduledService.class);
PendingIntent圆周率= PendingIntent.getService(YourActivity,0,I,0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+句号,PI);
 

其中,周期中之后,您希望闹钟响起毫秒的时间。

要取消报警的使用:

 如果(经理!= NULL)
        mgr.cancel(PI);
 

最后,这一切工作,你需要注册您的 ScheduledService 类作为服务。 在您的清单中添加本土特产品您的应用程序:

 <应用
    ... />
    ...
    <服务机器人:名称=。ScheduledService>
    < /服务>

< /用途>
 

这样的Andr​​oid操作系统将发射报警时,它的时间的照顾。即使其他应用程序正在运行或您的应用程序进程被终止。

I want my app to throw a notification after 10 seconds even if the app is not running. I have made use of the AlarmManager but unable to get the notification at the desired time. I am able to get a toast at that time but not the notification. Any help will be deeply appreciated.

This is my AlarmManager class:

public class AlarmManagerActivity extends Activity {

private AlarmManagerBroadcastReceiver alarm;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_manager);
    alarm = new AlarmManagerBroadcastReceiver();
    onetimeTimer();


}

@Override
protected void onStart() {
    super.onStart();
}


public void notifyN(){

    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new Notification.Builder(this)
            .setContentTitle("This is a sample notification")
            .setContentText("Subject")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_action_search, "Call", pIntent)
            .addAction(R.drawable.ic_action_search, "More", pIntent)
            .addAction(R.drawable.ic_action_search, "And more", pIntent).build();


    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}


public void onetimeTimer(){
    Context context = this.getApplicationContext();
    if(alarm != null){
        alarm.setOnetimeTimer(context);
        notifyN();

    }else{
        Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
}

}

And this is the AlarmManagerBroadcastReceiver:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

final public static String ONE_TIME = "onetime";

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    // Acquire the lock
    wl.acquire();

    // You can do the processing here update the widget/remote views.
    Bundle extras = intent.getExtras();
    StringBuilder msgStr = new StringBuilder();

    if (extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)) {
        msgStr.append("One time Timer : ");
    }
    Format formatter = new SimpleDateFormat("hh:mm:ss a");
    msgStr.append("abcs stop the instance" + formatter.format(new Date()));

    Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();

    // Release the lock
    wl.release();

}

public void setOnetimeTimer(Context context) {
    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.TRUE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi);

}
}

解决方案

Create a class (call it something like ScheduledService) and let it extend IntentService. In this class you'll do what you want to do when the alarm goes off.

public class ScheduledService extends IntentService {

public ScheduledService() {
    super("My service");
}

@Override
protected void onHandleIntent(Intent intent) {
    //Do something, fire a notification or whatever you want to do here
    Log.d("debug", "Ring Ring!");

}
}

then in you activity to start the alarm use the following:

AlarmManager mgr = (AlarmManager) YourActivity.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(YourActivity, ScheduledService.class);
PendingIntent pi = PendingIntent.getService(YourActivity, 0, i, 0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);

Where PERIOD is the time in milliseconds after which you want the alarm to goes off.

To cancel the alarm use:

if (mgr != null)
        mgr.cancel(pi);

Finally for all this to work you need to register your ScheduledService class as a Service. In your manifest add this tou your application:

<application
    ... />
    ...
    <service android:name=".ScheduledService" >
    </service>

</application>

This way the Android OS will take care of firing the alarm when it's time. even if another application is running or your app process is terminated.

这篇关于如何设置调度的通知中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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