Android 如何使用服务通知后台的应用程序
我正在写一个有服务的应用程序。该服务应该执行一些长时间运行的后台活动,比如获取地理坐标。现在,我想让这项服务在一段特定的时间间隔后通知应用程序需要做一些事情,比如报警。我对这一部分感到困惑。我没有得到,我如何才能使服务更新应用程序的一些行动,然后显示一些警报等 下面是我的代码 活动:Android 如何使用服务通知后台的应用程序,android,android-activity,broadcastreceiver,android-service,Android,Android Activity,Broadcastreceiver,Android Service,我正在写一个有服务的应用程序。该服务应该执行一些长时间运行的后台活动,比如获取地理坐标。现在,我想让这项服务在一段特定的时间间隔后通知应用程序需要做一些事情,比如报警。我对这一部分感到困惑。我没有得到,我如何才能使服务更新应用程序的一些行动,然后显示一些警报等 下面是我的代码 活动: public class BroadcastTest extends Activity { private static final String TAG = "BroadcastTest";
public class BroadcastTest extends Activity {
private static final String TAG = "BroadcastTest";
private Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent = new Intent(this, BroadcastService.class);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(
BroadcastService.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
private void updateUI(Intent intent) {
boolean gps = intent.getBooleanExtra("gps", false);
if (gps) {
String counter = intent.getStringExtra("counter");
String time = intent.getStringExtra("time");
// Log.d(TAG, counter);
// Log.d(TAG, time);
TextView txtDateTime = (TextView) findViewById(R.id.txtDateTime);
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
txtDateTime.setText(time);
txtCounter.setText(counter);
} else {
}
}
}
public class BroadcastService extends Service {
private static final String TAG = "BroadcastService";
public static final String BROADCAST_ACTION = "";
private final Handler handler = new Handler();
Intent intent;
int counter = 0;
GPSTracker gps;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
//Notification notification = new Notification(R.drawable.icon, "MyService", 1000);
//Intent notifIntent = new Intent(this, BroadcastTest.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, 0);
//notification.setLatestEventInfo(getApplicationContext(), "ForeGroundService", "Service is Running", pendingIntent);
//startForeground(1, notification);
}
@Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 10000); // 10 seconds
}
};
private void DisplayLoggingInfo() {
gps = new GPSTracker(getApplicationContext());
double latitude = 0, longitude = 0;
// check if GPS enabled
System.out.println(gps.isGPSEnabled);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
//gps.showSettingsAlert();
}
Log.d(TAG, "entered DisplayLoggingInfo");
intent.putExtra("time", Double.toString(latitude));
intent.putExtra("counter", Double.toString(longitude));
sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
}
}
服务:
public class BroadcastTest extends Activity {
private static final String TAG = "BroadcastTest";
private Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent = new Intent(this, BroadcastService.class);
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
@Override
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(
BroadcastService.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
private void updateUI(Intent intent) {
boolean gps = intent.getBooleanExtra("gps", false);
if (gps) {
String counter = intent.getStringExtra("counter");
String time = intent.getStringExtra("time");
// Log.d(TAG, counter);
// Log.d(TAG, time);
TextView txtDateTime = (TextView) findViewById(R.id.txtDateTime);
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
txtDateTime.setText(time);
txtCounter.setText(counter);
} else {
}
}
}
public class BroadcastService extends Service {
private static final String TAG = "BroadcastService";
public static final String BROADCAST_ACTION = "";
private final Handler handler = new Handler();
Intent intent;
int counter = 0;
GPSTracker gps;
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
//Notification notification = new Notification(R.drawable.icon, "MyService", 1000);
//Intent notifIntent = new Intent(this, BroadcastTest.class);
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifIntent, 0);
//notification.setLatestEventInfo(getApplicationContext(), "ForeGroundService", "Service is Running", pendingIntent);
//startForeground(1, notification);
}
@Override
public void onStart(Intent intent, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this, 10000); // 10 seconds
}
};
private void DisplayLoggingInfo() {
gps = new GPSTracker(getApplicationContext());
double latitude = 0, longitude = 0;
// check if GPS enabled
System.out.println(gps.isGPSEnabled);
if (gps.canGetLocation()) {
latitude = gps.getLatitude();
longitude = gps.getLongitude();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
//gps.showSettingsAlert();
}
Log.d(TAG, "entered DisplayLoggingInfo");
intent.putExtra("time", Double.toString(latitude));
intent.putExtra("counter", Double.toString(longitude));
sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
}
}
有谁能告诉我,在特定的时间间隔后(比如4小时后或50公里后)如何触发应用程序以显示通知这是我在后台服务onStartCommand函数中编写的内容
public class Xyz extends Service{
public int onStartCommand(Intent intent, int flags, int startId) {
TimerTask geo = new TimerTask(){
public void run(){
if(your condition)
{
Uri soundrui = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(getApplicationContext(),
AcitivitytoOpenOnNotificationClick.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent p = PendingIntent.getActivity(
getApplicationContext(), 0, intent, 0);
Notification msg = new NotificationCompat.Builder(
getApplicationContext())
.setContentTitle("Your App")
.setContentText("Your Notification")
.setContentInfo(""+count)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentIntent(p).build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify("App tag", 0, msg);
}
}
}
}
Timer t = new Timer();
t.schedule(geo/*TimerTask object*/,long delay_before_first_execution, long period );
return super.onStartCommand(intent, flags, startId);
}
//End of startcommand
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//Dont forget to cancel the timer.
t.cancel();
}
}
//End of class
我希望这会有所帮助。是否要从服务更新UI???是的,有点。即使应用程序未运行,我也要显示通知。您可以将其视为主屏幕通知栏中的报警通知通知??是的,这符合我的目的。我想通知用户他设置的警报谢谢,你能给我一些时间检查一下吗。我会通知你的,当然。慢慢来。soundrui没有用?是的,你可以用。我觉得它太刺激了,所以我把它去掉了。检查此链接。它给出了完美的解释。