Question

So currently I have a switch in an android application which when turned on creates a file (deletes the file when turned off). When I turn on the phone, the broadcast received checks to see whether the file exists or not. If it does, then it runs some setup stuff. The problem is that I create the application in the data directory (not the application's data directory). Therefore there are issues with permissions and I have to use su chmod to change the permissions access. I want to avoid this issue so I was looking at ContentProviders.

Should I go for a content provider or is there a simpler way given I don't need to read the file, just check whether it exists or not.

Could I create the file in the applications data space and access it from the broadcast receiver?

Était-ce utile?

La solution

Try a BroadcastReceiver that has data in it.

Send:

Intent intent = new Intent("my.action.string");
intent.putExtra("on", true);
sendBroadcast(intent);

Receive:

public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equals("my.action.string")){
     boolean state = intent.getExtras().getBoolean("on");
  }
}

Manifest:

<receiver android:name=".MyReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="my.action.string" />
    </intent-filter>
</receiver>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top