Launcher Part 3
How to set Wallpaper from apps?
Send Intent to start suitable Activities.
1. Without the option of set default wallpaper
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,getText(R.string.chooser_wallpaper));
startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
2. With the option of set default wallpaper
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
How receivers(Activities) should setup their AndroidManifest.xml ?
Updating your UI in non main-thread:
1. Without the option of set default wallpaper
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,getText(R.string.chooser_wallpaper));
startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
2. With the option of set default wallpaper
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
How receivers(Activities) should setup their AndroidManifest.xml ?
They all must setup user-permission for set wallpaper.
<uses-permission android:name="android.permission.SET_WALLPAPER" />
AndroidManifest.xml (..\packages\apps\gallery2)
<activity android:name="com.android.gallery3d.app.Wallpaper"
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
AndroidManifest.xml (..\packages\apps\launcher2)
<activity android:name="com.android.launcher2.WallpaperChooser">
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
AndroidManifest.xml (..\packages\wallpapers\livepicker)
<activity android:name="LiveWallpaperActivity">
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
How to select a picture to set a Wallpaper?
You can porting codes , such as …
InputStream in = new FileInputStream(new File("/mnt/sdcard/xxx.jpg"));
WallpaperManager wpm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE);
wpm.setStream(in);
And, other ideas : changing different wallpapers in a period of time.
Handler: transfer messages about updating ui in sub-thread to main-thread or make a timer for ui.
Runnable mTasks = new Runnable(){ ui codes you wanted to repeat…}
handler.postDelayed(mTasks, 500); // 500 is a time to repeat
Updating your UI in non main-thread:
Activity | Activity or others(Fragment) |
Handler and Message Or runOnUiThread(new Runnable(){ public void run(){ // update ui view } }); | Handler and Message |
Handler and Message:
1.
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case XXX.MSG:
// update ui view
break;
}
public void handleMessage(Message msg) {
switch (msg.what) {
case XXX.MSG:
// update ui view
break;
}
…
}
};
}
};
2.
class handler extends Handler
{
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case XXX.MSG :
// update ui view
break;
}
}
}
handler hd = new hander();
Special Class : The Difference between LocalService and RemoteService
Source: http://developer.android.com/guide/topics/fundamentals/services.htmlA Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
A service can essentially take two forms:
Started(LocalService)
A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
Bound(RemoteService)
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.
Regardless of whether your application is started, bound, or both, any application component can use the service (even from a separate application), in the same way that any component can use an activity—by starting it with an Intent. However, you can declare the service as private, in the manifest file, and block access from other applications. This is discussed more in the section about Declaring the service in the manifest.
LocalService
RemoteService
Part 3 is over
Thank you for your patient reading
No comments:
Post a Comment