0

在我的应用程序中,用户必须使用时间选择器填写 9 个不同的时间。这些时间存储在 SQLite 数据库中。我希望用户在达到其中一个时间时收到状态栏通知。

例子:

用户第一次填写时间为 09:00。如果是实时09:00,用户需要得到状态栏通知。我希望他们不需要启动应用程序,它必须是自动的。

今天在 stackoverflow 和其他网站上花了超过 4 个小时后,我没有成功。我尝试了以下代码,但没有成功。通知现在设置为 12:00。时间到了12:00,我没有收到消息。

主要活动:

    public class MainActivity extends Activity {
AlarmManager am;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent alarmIntent = new Intent(this, TimeAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pendingIntent); } }

TimeAlarm.class:

public class TimeAlarm extends BroadcastReceiver {

 NotificationManager nm;

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

        Intent startServiceIntent = new Intent(context, NotificationService.class);
        context.startService(startServiceIntent);

 }
}

NotificationService.class:

public class NotificationService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    handleIntent(intent);
    return START_NOT_STICKY;
}

private NotificationManager nm;
private WakeLock mWakeLock;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    mWakeLock.release();
}

private void showNotification() {


    NotificationCompat.Builder mBuilder = 
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Test")
            .setContentText("Test message");
    Intent resultIntent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());

}

private class PollTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        showNotification();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        stopSelf();
    }
}

private void handleIntent(Intent intent) {

    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
            "NotificationsService");
    mWakeLock.acquire();
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    if (!cm.getBackgroundDataSetting()) {
        stopSelf();
        return;
    }
    new PollTask().execute();
}

安卓清单:

    <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-permission
        android:name="com.test.app.permission.C2D_MESSAGE" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.test.app.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.test.app.TimeAlarm">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>
    </application>
</manifest>
4

1 回答 1

0

尝试将 START_NOT_STICKY 更改为 START_STICKY;。如果您的服务不是粘性的,则如果必须将其杀死以便为内存中的其他应用程序腾出空间,则 android 不会尝试重新启动它。

于 2013-09-12T21:33:12.440 回答