gpt4 book ai didi

java - 如何使用其他应用程序包名称将图像设置为按钮?

转载 作者:行者123 更新时间:2023-11-30 02:51:05 25 4
gpt4 key购买 nike

我面临如下所述的问题。

我在小部件上只有一个按钮。单击按钮会打开其他应用程序。现在我的问题是如何将打开的应用程序图像分配给小部件中的按钮。请帮助我。

这是我的主类。

{

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetMainProvider extends AppWidgetProvider {
// Activity a=new Activity();

public static String GET_ACTION = "Get_Action";

public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);

if (GET_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
}

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;


Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));

// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
//Which defines the UI of Widget
// initializing widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget1);

Intent intent = new Intent(context, WidgetOneActivity.class);
// intent.setAction(GET_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

}}}

这是我的另一个类,我在其中声明了程序包名称,基于该名称它将打开应用程序。

package com.purvotara.example.widget;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class WidgetOneActivity extends Activity {

/** Called when the activity is first created. */



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PackageInfo pkgInfo=new PackageInfo();
context.getPackageManager().getApplicationIcon(pkgInfo.packageName);
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);

}


}

也请帮我把动态设置图像到按钮的代码放在哪里。

这是我的 Android。 list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eightbitcloud.example.widget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="com.purvotara.example.widget.WidgetOneActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name="com.purvotara.example.widget.WidgetMainProvider"
android:label="@string/widget1name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@XML/widget1_info" />
</receiver>
</application>

</manifest>

请帮帮我极客们,我在等。

最佳答案

据我了解,您正在尝试将另一个应用程序图标分配给您的小部件按钮。你可以试试:

PackageManager packageManager = mContext.getPackageManager();
Drawable icon = packageManager .getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);

Drawable icon = getPackageManager().getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);

我在 WidgetOneActivity 类中添加了以下内容:

 public void onCreate(Bundle savedInstanceState) {      
super.onCreate(savedInstanceState);

Button button = null;

//YOU NEED TO INITIALIZE YOUR BUTTON HERE, YOUR BUTTON IS NULL
try {
Drawable icon;
icon = getPackageManager().getApplicationIcon("com.drdo.s.a.m.s");
button.setBackground(icon);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);

}
});

}

添加这个后,我的应用程序将关闭。

关于java - 如何使用其他应用程序包名称将图像设置为按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24139886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com