can i register the widget from java code instead o

2019-08-09 02:53发布

问题:

So here is the thing,i have an app,if the music is not playing(for which i am using service) and the user exits out of the activity then I want the service to stop and the app to stop too,which happens perfectly

Now i start the app again,everything works fine,except the widget stops receiving the broadcasts even though that same broadcasts is being received by another activity when it is supposed to.

Also strangely the widget is able to send broadcasts (it is only unable to receive it) so it seems like the broadcastreceivers are not being registered again(only for the widget).How can i register it again??I really have to use system.exit(),not using it is screwing up other things :(

This is the what i am calling in ondestroy()

if(ma.boolMusicPlaying1==false)
        {
            getActivity().getApplicationContext().stopService(new Intent(this.getActivity(), Music_service.class));
            ma.NP_List.clear();
            ma = null;
            System.gc();
            System.exit(0);
        }

So my guess is if i can register the widget through java file my problem would be solved.So can i register the widget through a java file??

in other words what is the java code for

receiver android:name="source.justanothermusicplayer.service.MyWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
         </receiver>