我正在为我的应用程序创建一个小部件。我需要以编程方式调用从数据库获取的数字。
我能够收到数据库值并拨打电话。
我不想在创建窗口小部件时进行调用....而只需在单击应用程序时进行调用...
我在这里发布我的WidgetService类......
@Override
public void onStart(Intent intent, int startId) {
Log.i(LOG, "Called");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.widget_layout);
//remoteViews.setTextViewText(R.id.update,"Random: " + String.valueOf(number));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent((Integer) null, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
myDB = UpdateWidgetService.this.openOrCreateDatabase("divertnumber", Context.MODE_PRIVATE, null);
Cursor cursor = myDB.query("diverted", null, null, null, null, null, null);
cursor.moveToLast();
phoneNumber = cursor.getString(cursor.getColumnIndex("divertno"));
cursor.close();
Toast.makeText(UpdateWidgetService.this, "FROM DATABASE "+phoneNumber, Toast.LENGTH_LONG).show();
//Uri uri = Uri.fromParts("tel", phoneNumber, null);
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+5556+","+1+","+1));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}