我有一个使用插件系统的应用。这些“插件”会发出一条广播消息,然后我的应用会收到该消息并使用附加的相关数据来注册该应用。
问题是我不知道在任何给定时间设备上安装了多少插件,所以我不知道要等待多少消息,而且我不知道有什么方法可以检查它是否是最后一条消息正在发送。
现在我正在通过等待三秒计时器完成伪装,允许插件注册三秒钟,但随着更多插件的开发,这不会是足够的时间。
如果所有插件都已完成注册,我该如何刷新我的UI?
PanelReceiver.java
package com.t3hh4xx0r.haxlauncher.menu.livepanel;
import java.io.ByteArrayOutputStream;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import com.t3hh4xx0r.haxlauncher.DBAdapter;
public class PanelReceiver extends BroadcastReceiver {
private static final String PANEL_REQUEST = "com.t3hh4xx0r.haxlauncher.PANEL_REQUEST";
private static final String PANEL_REGISTER = "com.t3hh4xx0r.haxlauncher.PANEL_REGISTER";
private static final String PANEL_UPDATE = "com.t3hh4xx0r.haxlauncher.PANEL_UPDATE";
boolean shouldAdd = true;
@Override
public void onReceive(Context context, Intent intent) {
String a = intent.getAction();
if (a.equals(PANEL_REGISTER)) {
Bundle b = intent.getExtras();
String author = b.getString("author_name");
String plugin = b.getString("plugin_name");
String packageN = b.getString("package_name");
String desc = b.getString("description");
String version = b.getString("version");
DBAdapter db = new DBAdapter(context);
db.open();
Cursor c = db.getAllPanels();
if (c.getCount() != 0) {
while (c.moveToNext()) {
if (c.getString(c.getColumnIndex("author")).equals(author) &&
c.getString(c.getColumnIndex("package")).equals(packageN)){
shouldAdd = false;
if (!c.getString(c.getColumnIndex("version")).equals(version)) {
db.removePanel(packageN);
shouldAdd = true;
}
}
}
}
if (shouldAdd) {
db.insertPanel(version, author, plugin, packageN, desc, image("ic_launcher", context, packageN), image("screencap", context, packageN));
}
db.close();
} else if (a.equals(PANEL_UPDATE)) {
Log.d("UPDATE", "OMFG!");
}
}
public static void requestPanels(Context context) {
Intent i = new Intent();
i.setAction(PANEL_REQUEST);
context.sendBroadcast(i);
}
public byte[] image(String imageName, Context c, String packageN) {
try {
Context fC = c.getApplicationContext().createPackageContext(packageN, Context.CONTEXT_IGNORE_SECURITY);
Resources res = fC.getResources();
Bitmap photo = drawableToBitmap(res.getDrawable(res.getIdentifier(imageName, "drawable", packageN)));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
return bos.toByteArray();
} catch (NameNotFoundException e) {
return null;
}
};
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
PanelMenuActivity.java
package com.t3hh4xx0r.haxlauncher.menu.livepanel;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.t3hh4xx0r.haxlauncher.DBAdapter;
import com.t3hh4xx0r.haxlauncher.R;
public class PanelMenuActivity extends Activity {
ListView lv;
static ArrayAdapter<String> a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panel_menu);
lv = (ListView) findViewById(R.id.panel_list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int pos,
long id) {
Intent i = new Intent(v.getContext(), PanelDetails.class);
Bundle b = new Bundle();
b.putInt("pos", ((PanelDetailHolder)a.getItemAtPosition(pos)).id);
i.putExtras(b);
startActivity(i);
}
});
GetPanelsTask t = new GetPanelsTask();
t.execute();
}
private ListAdapter buildAdapter(Context c) {
return new PanelAdapter(this, getPanels(c));
}
public ArrayList<PanelDetailHolder> getPanels(Context ctx) {
ArrayList<PanelDetailHolder> panelArray = new ArrayList<PanelDetailHolder>();
DBAdapter db = new DBAdapter(ctx);
db.open();
Cursor c = db.getAllPanels();
while (c.moveToNext()) {
try{
getPackageManager().getApplicationInfo(c.getString(c.getColumnIndex("package")), 0 );
} catch (NameNotFoundException e){
db.removePanel(c.getString(c.getColumnIndex("package")));
continue;
}
PanelDetailHolder p = new PanelDetailHolder();
p.author = c.getString(c.getColumnIndex("author"));
p.title = c.getString(c.getColumnIndex("name"));
p.desc = c.getString(c.getColumnIndex("desc"));
p.version = c.getString(c.getColumnIndex("version"));
p.id = c.getPosition();
panelArray.add(p);
}
c.close();
db.close();
return panelArray;
}
@Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.refresh:
GetPanelsTask t = new GetPanelsTask();
t.execute();
}
return false;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuinflate = new MenuInflater(this);
menuinflate.inflate(R.menu.panel_menu, menu);
return true;
}
public class GetPanelsTask extends AsyncTask<Object, View, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(PanelMenuActivity.this,
"Loading...","Checking for live panels...", true);
PanelReceiver.requestPanels(PanelMenuActivity.this);
};
@Override
protected Void doInBackground(Object... params) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
};
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
lv.setAdapter(buildAdapter(PanelMenuActivity.this));
}
}
class PanelDetailHolder {
String author;
String title;
String desc;
String version;
Drawable screenCap;
Drawable icon;
int id;
}
}
答案 0 :(得分:0)
如果所有插件都已完成注册,我该如何刷新我的UI?
根据定义,这是不可能的。您需要修改插件系统,以便: