我有一個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();
}
}