-1

尝试将我的 iOS 应用程序转换为 android,我知道我无法移植它,所以我从头开始编写它如何将此通知转换为 Android Java 代码?

-(IBAction)turnon {

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:28];
    [comps setMonth:9];
    [comps setYear:2012];
    [comps setHour:8];
    [comps setMinute:25];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *fireDate = [gregorian dateFromComponents:comps];

    UILocalNotification *alarm = [[UILocalNotification alloc] init];

    alarm.fireDate = fireDate;
    alarm.repeatInterval = NSDayCalendarUnit;
    alarm.soundName = @"msl.aiff";
    alarm.alertBody = @"This is a message..";
    alarm.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

我已经在网上搜索了大约 4 个小时,我认为这对于 Android 开发人员来说很简单,但因为我刚开始我只是不知道如何做到这一点。

非常感谢任何帮助!

4

2 回答 2

1

这就是您要寻找的:您可以使用警报管理器在特定时间显示通知,即使您的应用程序根本没有运行。

http://developer.android.com/reference/android/app/AlarmManager.html

特定时间的每日通知

这对以下有用:

使用 Alarmmanager 在特定时间启动服务

编辑见评论:

您可以为此使用 AlarmManager,首先为您自己创建某种接收器。

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent dailyUpdater = new Intent(context, MyService.class);
        context.startService(dailyUpdater);
        Log.d("AlarmReceiver", "started service");
    }
}

比您需要创建将显示通知的服务

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private NotificationManager mNM;


    private int NOTIFICATION = 546;

    public class LocalBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showNotification();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        mNM.cancel(NOTIFICATION);
        Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    private void showNotification() {
        Toast.makeText(this, "show notification", Toast.LENGTH_SHORT).show();

        //notification code here


    }
}

最后你需要设置闹钟:

private void setRecurringAlarm(Context context) {
    Calendar updateTime = Calendar.getInstance();
    updateTime.set(Calendar.HOUR_OF_DAY, 20);
    updateTime.set(Calendar.MINUTE, 30);

    Intent open = new Intent(context, AlarmReceiver.class);
    open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, open, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 5000, 10000, pendingIntent); 
}

在您进行测试运行之前,将您的接收器和服务添加到您的清单文件中:

<service android:name=".MyService"></service>
<receiver android:name=".AlarmReceiver"></receiver>
于 2012-10-17T14:24:14.837 回答
0

来自Android 开发者网站的一些小改动的复制粘贴

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
// Sets an ID for the notification, so it can be updated
int mId= 1;
mNotificationManager.notify(mId, mBuilder.build());
于 2012-10-17T14:21:26.217 回答