我正在尝试更新支持上下文语音命令的低频实时卡的经度和纬度(可以从XE 21.0更新中获得)。
由于我的服务呈现了实时卡并收听了位置更新,因此我不确定如何从服务本身添加上下文菜单。官方文档中的示例显示它们已添加到活动中。
这是我的代码,
public class Start_service extends Service {
private static final String LIVE_CARD_TAG = "LiveCardDemo";
public static double lati;
public static double longi;
public static float speed;
private LiveCard mLiveCard;
private RemoteViews mLiveCardView;
private final Handler mHandler = new Handler();
private final UpdateLiveCardRunnable mUpdateLiveCardRunnable =
new UpdateLiveCardRunnable();
private static final long DELAY_MILLIS = 3000;
@Override
public void onCreate() {
super.onCreate();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String provider = mLocationManager.getBestProvider(criteria, true);
boolean isEnabled = mLocationManager.isProviderEnabled(provider);
if (isEnabled) {
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null){
Geocoder geocoder = new Geocoder(Start_service.this.getBaseContext(), Locale.getDefault());
try {
lati= location.getLatitude();
longi=location.getLongitude();
speed=location.getSpeed();
}
catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
}
// Register the listener with the Location Manager to receive location updates
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
}
}
这是我的渲染低频真人卡的代码,这也是上面的服务类,现在我该如何为它添加一个上下文语音菜单?
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mLiveCard == null) {
// Get an instance of a live card
mLiveCard = new LiveCard(this, LIVE_CARD_TAG);
// Inflate a layout into a remote view
mLiveCardView = new RemoteViews(getPackageName(),
R.layout.main_layout);
// Set up the live card's action with a pending intent
// to show a menu when tapped
Intent menuIntent = new Intent(this, Sample.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
mLiveCard.setAction(PendingIntent.getActivity(
this, 0, menuIntent, 0));
// Set up initial RemoteViews values
mLiveCardView.setTextViewText(R.id.latitude_value,
getString(R. id.latitude_value));
mLiveCardView.setTextViewText(R.id.longitude_value,
getString(R.id.longitude_value));
mLiveCardView.setTextViewText(R.id.speed_value,
getString(R.id.speed_value));
// Publish the live card
mLiveCard.setVoiceActionEnabled(true);
mLiveCard.publish(PublishMode.REVEAL);
// Queue the update text runnable
mHandler.post(mUpdateLiveCardRunnable);
}
return START_STICKY;
}
@Override
public void onDestroy() {
if (mLiveCard != null && mLiveCard.isPublished()) {
//Stop the handler from queuing more Runnable jobs
mUpdateLiveCardRunnable.setStop(true);
mLiveCard.unpublish();
mLiveCard = null;
}
/// mlocManager.removeUpdates(mlocListener);
super.onDestroy();
}
/**
* Runnable that updates live card contents
*/
private class UpdateLiveCardRunnable implements Runnable{
private boolean mIsStopped = false;
/*
* Updates the card with a fake score every 30 seconds as a demonstration.
* You also probably want to display something useful in your live card.
*
* If you are executing a long running task to get data to update a
* live card(e.g, making a web call), do this in another thread or
* AsyncTask.
*/
public void run(){
if(!isStopped()){
mLiveCardView.setTextViewText(R.id.latitude_value,
String.valueOf(lati));
Log.e("updated", "updated latitude...............");
mLiveCardView.setTextViewText(R.id.longitude_value,
String.valueOf(longi));
mLiveCardView.setTextViewText(R.id.speed_value,
String.valueOf(speed));
// Always call setViews() to update the live card's RemoteViews.
mLiveCard.setViews(mLiveCardView);
// Queue another score update in 30 seconds.
mHandler.postDelayed(mUpdateLiveCardRunnable, DELAY_MILLIS);
}
}
答案 0 :(得分:1)
您可以使用类似于Compass sample的方式对此进行建模:
IBinder
,其中包含更新实时卡的方法。