English 中文(简体)
通知作为服务
原标题:android notification as service

我不想在具体时间申请临时通知。 具体时间一米计算法非常复杂,但现在情况并非如此。

每天的通知时间各不相同:

12/01/12 | 13/01/12 | 14/01/12
n1:02:00 | n1:02:05 | n1:02:06
n2:07:00 | n2:07:08 | ...
n3:12:00 | n3:12:02 | ...
n4:14:00 | n4:13:59 | ...
n5:20:00 | n5:20:01 | ...

我创建了两个活动类别,用于在特定时间进行通知:

private void createNotification(final String contentTitle, final String contentText,
        final String tickerText) {

    calculateTimes();
    setFirstMillis(getNextTime());

    createStatusBarNotification(contentTitle, contentText, tickerText);

    Notification note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis());

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

    note.setLatestEventInfo(this, contentTitle, contentText, intent);

    note.vibrate = new long[]{1000,1000,1000,1000,1000};
    note.defaults |= Notification.DEFAULT_LIGHTS;

    note.sound = Uri.parse(getAlarmTone());

    note.flags |= Notification.FLAG_AUTO_CANCEL;
    note.flags |= Notification.FLAG_INSISTENT;
    notificationManager.notify(NOTIF_ID, note);
}

private void createStatusBarNotification(final String contentTitle,
        final String contentText, final String tickerText) {

    Date date = new Date(getFirstMillis());

    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            createNotification(contentTitle, contentText, tickerText);
        }
    };
    timer.schedule(timerTask, date);
}

我开始采用以下方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    ...
    ...
    createStatusBarNotification("Title", "Text",
            "tickerText");

}

但我要开始这项通知。

我试图打造一个新班子。

public class StartNotificationService extends Service

但我不知道我需要在这个类别中写什么,以及这一类别在何时和从哪里运作。


最新情况:

我制定了新的活动方法,我从Create方法中呼吁采用。

public void setOneTimeAlarm() {
      Intent intent = new Intent(this, StartNotificationService.class);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);
      am.set(AlarmManager.RTC_WAKEUP,
        System.currentTimeMillis() + (5 * 1000), pendingIntent);
}

1. 启动认证服务班:

public class StartNotificationService extends BroadcastReceiver {

    private static final int NOTIF_ID = 1234;

    private Pror pror;
    private Context cntx;
    private NotificationManager notificationManager;

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

        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


      createStatusBarNotification("Title", "Text",
            "tickerText");
    }

    private void createStatusBarNotification(final String contentTitle,
            final String contentText, final String tickerText) {

        Date date = new Date(pror.getFirstMillis());
        Log.i("date", date.toString());

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                createNotification(contentTitle, contentText, tickerText);
            }
        };
        timer.schedule(timerTask, date);
    }

    private void createNotification(final String contentTitle, final String contentText,
            final String tickerText) {

        pror.calculateVak();
        pror.setFirstMillis(pror.getNextVak());

        createStatusBarNotification(contentTitle, contentText, tickerText);

        Notification note = new Notification(android.R.drawable.btn_star_big_on, tickerText, System.currentTimeMillis());

        PendingIntent intent = PendingIntent.getActivity(cntx, 0, new Intent(cntx, VakActivity.class), 0);

        note.setLatestEventInfo(cntx, contentTitle, contentText, intent);

        note.vibrate = new long[]{1000,1000,1000,1000,1000};
        note.defaults |= Notification.DEFAULT_LIGHTS;

        note.sound = Uri.parse(pror.getAlarmTone());

        note.flags |= Notification.FLAG_AUTO_CANCEL;
        note.flags |= Notification.FLAG_INSISTENT;
        notificationManager.notify(NOTIF_ID, note);
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 我想从活动到启动发放通知服务时发放变量。 我怎样做?

问题很可变。 我的这种变数是活动(现在的偏好,......),我想起起起“启动通知服务”类的例子。

问题回答

First you should use alarm manager and use alarm in service because when service running in background, it will check time when u want to show notification and show notification. i think it can help something. thanx

考虑使用AlarmManager: 根据你的算法安排你计算的时间,并在发出警报时发出通知。 见http://developer.android.com/vis/android/app/AlarmManager.html。

如果你想在服务执行或活动范围内制造警报,取决于如何重新计算时间。

更新(以下评论): 为什么不给你写上假变数的价值(证明或证明人类型显示的)对共同的偏好的价值,在你们需要价值时,再读到共同的偏好。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签