我对android开发非常陌生。
我有一个应用程序,读取从usb到串行连接每秒钟。然后,应用程序用新的数据更新MainActivity的UI。我的应用程序还有一个小部件,它也是用新的数据更新的。但是,当onDestroy()方法被调用时,这个方法工作得很好,小部件停止工作。
我已经看到了一些应用程序,小部件继续工作和更新,即使应用程序从未启动。至少据我所知没有。
这是怎么做的?我希望我的小部件在没有应用程序运行的情况下运行和运行。这个是可能的吗?
更新:
下面是MainActivity的onCreate()的一个片段
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final HandlerThread hTh = new HandlerThread("HT");
hTh.start();
final long oneScd = 1000;
final Handler handler = new Handler(hTh.getLooper());
Runnable updateDisplay = new Runnable(){
@Override
public void run() {
updateDisplay(GlobalSpeed._json);
}
};
Runnable eachSec = new Runnable() {
@Override
public void run() {
runOnUiThread(updateDisplay);
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(eachSec, oneScd);下面是我的AppWidgetProvider代码:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
try {
updateWidgetContent(context, appWidgetManager);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed", e);
}
}
public void updateWidgetContent(final Context context, final AppWidgetManager appWidgetManager) {
Intent launchAppIntent = new Intent(context, SoleWidgetProvider.class);
service = PendingIntent.getActivity(context, 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
hTh.start();
final long oneScd = 4000;
final Handler handler = new Handler(hTh.getLooper());
Runnable eachSec = new Runnable(){
@Override
public void run() {
String js;
js = GlobalSpeed._json;
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.sole_widget_layout);
remoteView.setTextViewText(R.id.wSpeed, js);//"Speed: " + String.valueOf(s.realSpeed())
ComponentName componentName = new ComponentName(context, SoleWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteView);
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(eachSec, oneScd);
}下面是我的服务,它从串行通信获得响应并设置GlobalSpeed._json值。
public class WidgetUpdateService extends Service {
public static final String DEBUG_TAG = "SoleWidgetService";
public static String json;
private SerialComunicator serComm;
private Context context;
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Log.d(DEBUG_TAG, "Sevice Started");
serComm = new SerialComunicator(this);
buildUpdate();
}
private void buildUpdate(){
HandlerThread hTh = new HandlerThread("SHT");
final long oneScd = 4000;
hTh.start();
final Handler handler = new Handler(hTh.getLooper());
Runnable readSerial = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
GlobalSpeed._json = serComm.readDataFromSerial();
handler.postDelayed(this, oneScd);
}
};
handler.postDelayed(readSerial, oneScd);
}
public void displayToast(String msg){
Toast.makeText(this, serComm.readDataFromSerial(), Toast.LENGTH_SHORT);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}}
希望这是足够的代码。
这一切都很好,当我进入主屏幕时,应用程序的onStop()方法被称为“它有效”,小部件继续更新。但是,当我将应用程序从最近的应用程序中删除并调用onDestroy()方法时,小部件将停止更新。
我希望小部件的功能独立于应用程序。我有这个可能吗?我知道一些小部件,似乎是独立于其应用程序运行的。
发布于 2013-07-11 22:56:08
您需要编写一个在后台运行的服务,然后通过意图更新小部件(以及应用程序)。
在android中,当一个应用程序不在前台时,它会被暂停,所以任何运行的代码都会被暂停。
服务独立于应用程序。读一读有关服务的文章。您可以让它在引导时启动,或者让小部件在需要数据时触发服务。
需要考虑的一件事是,每秒钟更新一次数据会耗尽电池。小部件通常用于偶尔更新数据(每30分钟、每小时等),而不是每秒钟更新一次。
https://stackoverflow.com/questions/17604452
复制相似问题