0

2 개의 버튼이있는 간단한 위젯이 있습니다. 아무 위젯 버튼을 클릭해도 동일한 활동이 열리지만 다른 매개 변수 인 을 열어야합니다. 모든 것이 잘 작동 : 클릭 버튼 (1) 활동을 열고 버튼을 2 활동을 엽니 클릭 PARAM 1을 통과하고 내가 함께 전달 된 매개 변수를 확인 PARAM 2. 전달 : 문제는 내가 안드로이드 홈을 누르면 시작Android 멀티 버튼 위젯 : Android 홈 브레이크 위젯 버튼 신분 확인

getIntent().getStringExtra("mode") 

버튼을 눌러 앱을 닫습니다. 그 후에 앱 흐름은 어떤 버튼을 눌렀는지 인식하지 못합니다. 하지만 뒤로 버튼으로 앱을 닫으면 모든 것이 좋습니다.

내 생각에 홈을 누르고 위젯 버튼 중 하나를 클릭하면 가장 최근의 인 텐트가 클릭 된 버튼에 해당하는 의도를 사용하는 대신 사용됩니다. 시도

  1. 는 intent.setAction()
  2. 시도 가능한 모든 플래그 대신 FLAG_CANCEL_CURRENT
  3. 시도 활동 발사 모드를 통해 의도에 작업을 추가하기 : 지금까지 시도했습니다 무엇

    하나 상위 및 단일 작업. 싱글 작업에는 다른 문제가있어서 싱글 탑을 고집합니다.

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_home); 
    
        Intent intent1 = new Intent(context, MainActivity.class); 
        intent1.putExtra("mode", "1"); 
        Intent intent2 = new Intent(context, MainActivity.class); 
        intent2.putExtra("mode", "2"); 
    
        PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, 
          intent1, PendingIntent.FLAG_CANCEL_CURRENT); 
        PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1, 
          intent2, PendingIntent.FLAG_CANCEL_CURRENT); 
    
        remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent1); 
        remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2); 
    
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 
    } 
    

    매니페스트 :

    <receiver 
        android:name="com.testwidgetbuttons.HomeWidgetProvider" 
        android:icon="@drawable/ic_launcher" 
        android:label="Example Widget" > 
        <intent-filter> 
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
        </intent-filter> 
    
        <meta-data 
         android:name="android.appwidget.provider" 
         android:resource="@xml/widget" /> 
    </receiver> 
    

    위젯 XML :

    <?xml version="1.0" encoding="utf-8"?> 
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
        android:initialLayout="@layout/widget_home" 
        android:minHeight="72dp" 
        android:minWidth="146dp" 
        android:updatePeriodMillis="1800000" > 
    
    </appwidget-provider> 
    

    알려 주시기 바랍니다

여기의 onUpdate 코드입니다. 감사합니다. .

답변

1

홈 버튼을 눌렀을 때 활동이 계속 실행 중이기 때문에 MainActivity를 시작하려는 의도는 활동의 onCreate() 메소드를 통해 읽지 못합니다.

As described here 기존 활동 인스턴스에 새 인 텐트를 전달할 때 대신 Activity.onNewIntent()을 사용하십시오.

이전 인스턴스를 완료하고 다시 만들고 싶다면 FLAG_ACTIVITY_CLEAR_TOP launch flag을 사용자의 의도로 지정할 수 있습니다.

+0

Paul, 두 점은 별도로 작동하지 않았지만 두 점을 모두 업데이트하면 (onNewIntent 및 깃발을 깨끗하게 맨 위) 작업이 수행되었습니다. 고맙습니다. – Gena