如何从接收器呼叫服务

时间:2012-06-22 07:49:12

标签: android

在我的应用程序中,我想从Receiver呼叫服务。

这是我的NotificationReceiver.java

 public class NotificationReceiver extends BroadcastReceiver {

    NotificationReceiver _this = this;
    private static Context _context;
    public static final String ACTION_REFRESH_SCHEDULE_ALARM = "com.example.receiver.ACTION_REFRESH_SCHEDULE_ALARM";
    String SERVER_URL = "http://192.168.1.7:8080";

    @Override
    public void onReceive(final Context context, Intent intent) {
        _context = context;
        initClient();
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(_context);


        if (settings.getString("userId",null) != null){

            gotNotification(new ClientListener(){
                @Override
                public void onSuccess(String response) {
                    try {
                        JSONObject object = new JSONObject(response);

                        System.out.println("Service object = "+ object);


                    } catch (JSONException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
                @Override
                public void onFail(int error) {

                }
                @Override
                public void onCancel() {

                }

            },settings.getString("userId",null));
        }
    }

    private void initClient(){
        NotifierRestClient.init(SERVER_URL + "/api");
    }

    private void gotNotification (final ClientListener clientListener, final String uId) {
        new Thread() {
            @Override
            public void run() {
                try {
                    final String responseText = NotifierRestClient.getUserNotifications(uId,null);
                    if (clientListener != null) {
                        clientListener.onSuccess(responseText);
                    }
                } catch (final Exception e) {
                    e.printStackTrace();
                    if (clientListener != null) {
                        clientListener.onFail(505);
                    }
                }
            }
        }.start();
    }
}

这是我的NotifiactionService.java

public class NotificationService extends Service {

    private AlarmManager alarmManagerPositioning;
    private PendingIntent pendingIntent;

    @Override
    public void onCreate() {
        super.onCreate();

        alarmManagerPositioning = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        final Intent intentToNotificate = new Intent(NotificationReceiver.ACTION_REFRESH_SCHEDULE_ALARM);
        pendingIntent = PendingIntent.getBroadcast(this, 0, intentToNotificate, 0);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        try {

            long notificationInterval = 5002;

            alarmManagerPositioning.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), notificationInterval, pendingIntent);
        } catch (NumberFormatException e) {
            Toast.makeText(this, "error running services: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "error running services: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

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

    @Override
    public void onDestroy() {

        this.alarmManagerPositioning.cancel(pendingIntent);


    }
}

我的问题是如何在NotificationReceiver中调用NotificationService并更改间隔?任何帮助都会有用,谢谢

1 个答案:

答案 0 :(得分:4)

使用

在接收器中启动服务
Intent in = new Intent(context,NotificationService.class);
in.putExtra("interval",5001);
context.startService(in);

在您的服务代码中

long notificationInterval = intent.getLongExtra("interval", defaultValue)

从您的接收方获取数据,请使用onStartCommand(Intent intent, int flags, int startId),onStart已被弃用。