Android 应用程序小部件错误

发布于 2024-12-26 02:09:09 字数 2294 浏览 4 评论 0原文

我正在为我的应用程序创建一个小部件。我需要以编程方式调用从数据库获取的号码。

我能够接收数据库值并拨打电话。

我不想在创建小部件时进行调用...而必须在单击应用程序时进行调用...

我在这里发布我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文