안녕하세요 저는 새 데이터를 데이터베이스에 입력 할 때마다 알림 표시 줄에 알림을 받아야하므로 안드로이드에 뉴스 애플리케이션을 구축하고 있습니다. 그래서 연구 후 나는 브로드 캐스트 리시버와 알람 매니저를 사용해야 만한다는 것을 발견했다. 하지만 내가 이해할 수없는 것은 응용 프로그램이 닫힐 때 새로운 데이터가 입력된다는 것을 어떻게 알 수 있습니까? 알람 관리자가 새 데이터 확인을 위해 애플리케이션을 활성화하는 방법을 5 분마다 말하겠습니까? 고맙습니다.브로드 캐스트 수신기 및 알람 관리자
1
A
답변
1
정확하게 이해하면 별도의 백그라운드 작업으로 수행해야하므로 새 데이터를 확인하기 위해 전체 애플리케이션을 실행할 필요가 없습니다.
필요한 것은 AlarmManager에 등록 된 경보이며, 경보가 발령 될 때 호출되는 BroadcastReceiver입니다. 수신자는 새 데이터를 확인하고 조건이 충족되면 알림 활동을 시작할 인 텐트를 실행합니다.
데이터베이스 액세스에 더 많은 시간이 필요한 경우 수신자가 시작한 서비스에서 수행하는 것이 좋습니다.
cwac-wakeful (https://github.com/commonsguy/cwac-wakeful)을 사용하여 코딩을 저장하고 (일부 공통적 인 문제를 해결할 수 있음) 데이터베이스 검사 서비스가 작업을 수행하는 동안 계속 깨어 있는지 확인합니다. 매우 잘 문서화되어 있으며 몇 가지 예가 있습니다.
읽을만한 가치가있는 이론은 http://www.vogella.com/articles/AndroidServices/article.html입니다. 대부분 서비스에 관한 내용이지만 알람에 대한 설명도 있습니다.
2
이 프로젝트는 AlarmManager를 사용하여 시간을 설정 한 후 10000 초마다 알림을 표시 할 수 있습니다.
AlarmActivityActivity.java :
package co.in.technoguys.AlarmActivity;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmActivityActivity extends Activity {
int notifyID=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
TimePicker tp=(TimePicker)findViewById(R.id.timePicker1);
DatePicker dp=(DatePicker)findViewById(R.id.datePicker1);
Calendar Cal = Calendar.getInstance();
Cal.set(Calendar.YEAR, dp.getYear());
Cal.set(Calendar.MONTH, dp.getMonth());
Cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
Cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
Cal.set(Calendar.MINUTE, tp.getCurrentMinute());
Cal.set(Calendar.SECOND, 00);
long when=Cal.getTimeInMillis();
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,when,20000,pendingIntent);
Toast.makeText(this, "Notification Triggered on"+tp.getCurrentHour()+":"+tp.getCurrentMinute(),
Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java :
package co.in.technoguys.AlarmActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
int notifyID=1;
@Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent();
PendingIntent pi=PendingIntent.getBroadcast(context,0,i,0);
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Notification Triggered",System.currentTimeMillis());
CharSequence from="Remainder";
CharSequence message="Meeting Starts at 9 A.M";
notify.setLatestEventInfo(context, from, message, pi);
nm.notify(notifyID,notify);
}
}
main.xml에 :
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/timePicker1"
android:layout_alignLeft="@+id/timePicker1"
android:layout_marginBottom="27dp" />
<Button
android:id="@+id/startAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_below="@+id/timePicker1"
android:layout_marginTop="36dp"
android:onClick="startAlert"
android:text="@string/count" />
</RelativeLayout>
,개
의 AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.in.technoguys.AlarmActivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AlarmActivityActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
관련 문제
- 1. 프래그먼트 및 브로드 캐스트 수신기
- 2. Android에서 브로드 캐스트 수신기 및 서비스
- 3. sendMultiPartTextMessage가있는 브로드 캐스트 수신기
- 4. 브로드 캐스트 수신기
- 5. 의도 및 브로드 캐스트 수신기 문제
- 6. android의 브로드 캐스트 수신기 및 ResultReceiver
- 7. LocationPoller 및 여러 개의 브로드 캐스트 수신기
- 8. 브로드 캐스트 수신기 및 위젯 시간대
- 9. 브로드 캐스트 수신기 - SMS 및 전화
- 10. 브로드 캐스트 수신기 및 인터넷 연결
- 11. Android 및 iPhone의 브로드 캐스트 수신기/서비스
- 12. 브로드 캐스트 수신기 시작 오류
- 13. 이상한 브로드 캐스트 수신기 동작
- 14. Nexus 7의 브로드 캐스트 수신기
- 15. 비동기 클라이언트 브로드 캐스트 수신기
- 16. 다른 수신기 (부팅 수신기)에서 브로드 캐스트 수신기 시작하기
- 17. 브로드 캐스트 수신자가 트리거를 수신하지 않음 알람
- 18. 처리기가없는 브로드 캐스트 수신기 (모범 사례)
- 19. 응용 프로그램 전체에 걸려있는 브로드 캐스트 수신기
- 20. 브로드 캐스트 수신기 안쪽에 스레드 안드로이드
- 21. 여러 오디오 이벤트가있는 브로드 캐스트 수신기
- 22. 브로드 캐스트 이벤트, 알람 기능 android
- 23. 동일한 의도의 다중 브로드 캐스트 수신기
- 24. 유닛 테스트 용 브로드 캐스트 수신기
- 25. 특정 앱에서 Android 브로드 캐스트 수신기 제한
- 26. IntentService의 브로드 캐스트 수신기 (서비스가 IntentReceiver를 유출했습니다)
- 27. 실행중인 프로세스를 확인하는 브로드 캐스트 수신기?
- 28. 보낸 SMS 메시지의 Android 브로드 캐스트 수신기?
- 29. ActionBar의 브로드 캐스트 수신기 및 탭 UI 개발
- 30. 브로드 캐스트 수신기가있는 Android 알람 관리자가 매니페스트가 아닌 코드로 등록되었습니다.