Android 应用程序小部件错误
我正在为我的应用程序创建一个小部件。我需要以编程方式调用从数据库获取的号码。
我能够接收数据库值并拨打电话。
我不想在创建小部件时进行调用...而必须在单击应用程序时进行调用...
我在这里发布我的 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;
}
I'm creating a widget for my application.I need to programatically make a call to a number which is get from a database.
I'm able to receive the database value and make the call.
I don't want to make the call while the widget is created....rather have to make calls whenever the application is clicked...
I'm posting my WidgetService class here...
@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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论