我在Android上创建了一个具有MySql服务器的应用程序。如果文件中存在任何数据,我想要通知(用户有工作将其全部删除但是在验证之后)。所以,我需要一个好的后台工作者,在一小时后激活,检查是否有任何数据,发送通知,关闭自己,然后在一小时后再次激活。 一小时的时间可以更改。我使用AsyncTask进行下载(不可避免)。我擅长发送通知和使用AsyncTask。
我有点懒,所以在确认它将完成我的任务之前没有做过任何实验。
我认为它可能会使用服务类。请提供详细信息。在告诉使用任何 Github库时请提供完整的教程,因为我是Github的新手。
谢谢,
你的尊敬,
印度最年轻的Android应用程序开发人员
答案 0 :(得分:0)
我找到了解决方案Myself,因此决定帮助其他任何新程序员。这是 -
<强> AlarmReceiverLifeLog.java 强>
public class AlarmReceiverLifeLog extends BroadcastReceiver {
private static final String TAG = "LL24";
static Context context;
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Alarm for LifeLog...");
Intent serviceIntent = new Intent(context,MyReciever.class);
context.startService(serviceIntent);
}
}
<强> MyReciever.java 强>
public class MyReciever extends Service {
int mStartMode;
IBinder mBinder;
boolean mAllowRebind;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Your Method to get Data from Server
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
}
@Override
public void onDestroy() {
}
//method to show notification to be called when you finally decide that you have to notify the user
public void showNotification(String title,String message){
Log.d("Service","Going to show notification");
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle(title)
.setContentText(message);
Intent notificationIntent = new Intent(this, NavigationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
在 AndroidManifest.xml
中添加此内容 <receiver android:name=".AlarmReceiverLifeLog" >
</receiver>
<service android:name=".MyReciever" />
激活接收器
Intent ll24 = new Intent(this, AlarmReceiverLifeLog.class);
PendingIntent recurringLl24 = PendingIntent.getBroadcast(this, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 0, AlarmManager.INTERVAL_HALF_DAY, recurringLl24);//For waking it after each 12hrs.