2012-06-22 2 views
1

내 응용 프로그램에서 수신기에서 서비스를 호출하고 싶습니다.수신기에서 서비스 호출하는 방법

이 내 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(); 
    } 
} 

그리고이

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를 호출 간격을 변경하는 방법을 내 NotifiactionService.java입니까? 어떤 도움이

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

는 수신기에서 데이터를 얻기 위해, onStartCommand(Intent intent, int flags, int startId)을 사용하십시오, 서비스 코드에서

Intent in = new Intent(context,NotificationService.class); 
in.putExtra("interval",5001); 
context.startService(in); 

를 사용하여 수신기에 유용 감사

+0

나는 Intent를 = new Intent (context, NotificationService.class); in.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); context.startService (in);를 사용하여 수신기에서 서비스를 시작할 수 있다고 믿는다. ?? – iNan

+0

맞습니다.하지만 서비스를 시작하기 만하면됩니다. 서비스를 요청하고 알림 간격을 변경해야합니다. – fish40

+0

서비스를 요청하는 중 직면 한 문제는 무엇입니까? 예외? 오류? –

답변

4

서비스 시작이 될 것이다, ONSTART는 더 이상 사용되지 .

+0

고마워요 – fish40

+0

당신이 가장 환영합니다 :) – iNan

관련 문제