2016-07-08 2 views
1

Android Wear 앱에서 내 서비스를 호출하지 내가 몇 번 알림을 만들이 응용 프로그램을 원하는 (내 서비스를 호출 할이 코드에서 알림을 만들기위한 매 15 분)알람 관리기는 내가 Android Wear 앱을 만들

AlramManager에 내 서비스를 등록하면 OS에서 생성 된 작업 (adb shell dumpsys alarm > dump.txt)을 볼 수는 있지만 서비스가 실행되었음을 알리는 알림 또는 로그를 볼 수 없습니다. startService으로 서비스를 테스트하고 알림 및 로그가 올바르게 작동하므로 어디에서 문제가 있는지 알 수 없습니다. 여기 내가 사용하는 코드는 다음과 같습니다 알람 관리기에 서비스를 등록

MyAlarmService.java

public class MyAlarmService extends Service { 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d("From serivce","Service run once"); 

     // Create notification 
     Context context = this; 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setContentTitle(context.getString(R.string.NotificationTitle)) 
      .setSmallIcon(R.mipmap.ic_launcher_hafez) 
      .setContentText("Some text"); 
     Notification secondPage = 
      new NotificationCompat.Builder(context) 
        .setContentTitle(context.getString(R.string.NotificationTitle)) 
        .setContentText("Some text") 
        .build(); 

     NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender(); 
     extender.addPage(secondPage); 
     builder.extend(extender); 

     NotificationManagerCompat mgr = NotificationManagerCompat.from(context); 
     int NotificationId = new Random().nextInt(5200); 
     mgr.notify(NotificationId, builder.build()); 
    } 

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

의 AndroidManifest.xml

... 
<service android:name=".MyAlarmService" /> 
... 

코드 : dump.txt

Context context = getActivity().getApplicationContext(); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
Intent intent = new Intent(context, MyAlarmService.class); 
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent); 

내용

Current Alarm Manager state: 

... 

Batch{91609e num=3 start=7228838 end=7233540 flgs=0x8}: 

... 

    RTC_WAKEUP #0: Alarm{18a777f type 0 when 1467992333472 org.rashno.mypackagename} 

     tag=*walarm*:org.rashno.mypackagename/.MyAlarmService 

     type=0 whenElapsed=-6s582ms when=2016-07-08 20:08:53 

     window=+11m15s0ms repeatInterval=900000 count=0 flags=0x0 

     operation=PendingIntent{397c34c: PendingIntentRecord{de77f95 org.rashno.mypackagename broadcastIntent}} 

... 

    Alarm Stats: 
    ... 
    u0a57:org.rashno.mypackagename +9ms running, 1 wakeups: 

    +9ms 1 wakes 1 alarms, last -1m33s417ms: 

     *walarm*:org.rashno.mypackagename/.MyAlarmService 
    ... 
    ... 
+0

제안 사항 (아직 * 해결책이 아닙니다 : ^) : 매니페스트에서 서비스를'exported = "true"'로 설정해보십시오. – String

+0

@String 시도해 보았지만 결과는 이전과 동일합니다 –

답변

1

서비스의 경우 은 getService을 사용하여 만들어야합니다.

PendingIntent alarmIntent = PendingIntent.getService(context, 0, intent, 0); 
+0

고맙습니다. 내 문제가 해결되었습니다. 다음 2 시간 전까지 현상금을 줄 수는 없어요. –

+0

AlarmManager를 (setRepeating 또는 set을 통해) 다시 시도해보십시오. 서비스가 두 번째로 실행되지 않습니다. 왜 이런 일이 발생하는지 알고 계십니까? –

+0

서비스가 이미 생성 된 경우 'onCreate'는 다시 호출되지 않습니다. 'onStartCommand'를 사용하거나'BroadcastReceiver'를 사용하도록 전환해야합니다. – tachyonflux

관련 문제