0

我使用 AlarmManager 启动服务,但由于某种原因,我无法确定该服务没有启动。

我的 onResume() 中用于启动服务的代码是:

Intent appIntent = new Intent(cxt, NotificationService.class);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            PendingIntent penIntent = PendingIntent.getService(this, 0,
                    appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.cancel(penIntent);

            penIntent = PendingIntent.getService(this, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, 5);
            cal.set(Calendar.MINUTE, 00);
            cal.set(Calendar.SECOND, 00);

            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
                    10 * 1000, penIntent);

清单包含以下内容:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
     android:debuggable="true"
     android:icon="@drawable/icon"
     android:label="@string/app_name" >
      <service
          android:name="de.brillow.mensa.Speiseplan$NotificationService"
          android:enabled="true" />

最后服务本身看起来像这样:

public class NotificationService extends Service {

        private WakeLock mWakeLock;

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

        private void handleIntent(Intent intent) {
            // obtain the wake lock
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "wakelocktag");
            mWakeLock.acquire();

            Log.i("PowerManager", "acquired");

            // do the actual work, in a separate thread
            new PollTask().execute();
        }

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

            @Override
            protected Void doInBackground(Void... params) {
                // do stuff!
                Log.i("Background", "stuff");
                return null;
            }


            @Override
            protected void onPostExecute(Void result) {
                // handle your data
                int id = 001;
                Log.i("Noti", "fication");
                NotificationManager mNotifyMng = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                        Speiseplan.this).setSmallIcon(R.drawable.icon)
                        .setContentTitle("Test").setContentText("Test!");

                mNotifyMng.notify(id, mBuilder.build());
                Log.i("Notification", "sent");
                stopSelf();
                Log.i("Service", "stopped");
            }
        }


        @Override
        public void onStart(Intent intent, int startId) {
            handleIntent(intent);
        }


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


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

由于某种原因,通知和 Log.i 消息都没有出现。我希望你能帮忙。

提前致谢!

4

1 回答 1

1

你不应该使用 a 的AsyncTask内部ServiceAsyncTask应该只用于将结果返回到当前活动的任务,即它应该在Activityor中定义Fragment。这就是它的设计目的。

doInBackground(...)在您的情况下,您可以将所有内容onPostExecute(...)放入onCreate(...)您的服务中。如果需要,请使用线程。如果您需要在 an 中可视化您的服务结果Activity,请调用 anIntent并将任何细节传递给 extras,例如

服务中的调用...

Intent intent=new Intent(context,VisualizingActivity.class);
intent.putExtra("A", "Data A");
intent.putExtra("B", "Data B");
context.startActivity(intent);

活动中的处理...

String A= intent.getStringExtra("A");
String B= intent.getStringExtra("B");

如果您只想创建Notifications,您可以直接从服务中执行此操作。不需要AsyncTaskor Activity

希望这会有所帮助……干杯!

于 2013-04-04T19:03:20.377 回答