我有一个Android收音机不会自动启动应用程序,但它可以从窗口小部件启动应用程序。我想创建一个小部件来启动我选择的各种应用程序,但我希望它们只能打开其他应用程序,然后关闭它们或在后台打开,因此一旦所有应用程序启动,主屏幕仍显示。我已经有一个简单的AppWidgetProvider我正在使用从另一个项目:Android创建窗口小部件,以启动其他应用程序,但在后台
public class AppStarterWidget extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
final int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int widgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.setInt(R.id.btnStartActivity, "setBackgroundResource",
R.drawable.circle_off);
try {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setComponent(new ComponentName("com.my.app", "Qualified name"));
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.btnStartActivity, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
//handler.postDelayed(this, 10000);
} catch (ActivityNotFoundException e) {
Toast.makeText(context.getApplicationContext(),
"There was a problem loading the application: ",
Toast.LENGTH_SHORT).show();
}
}