Set up VPN on Android in less than 5 minutes
2010-10-22 09:00:19
Setting up VPN on Android, or any smart phone for that matter, can be a bit of a pain. It's not ideal having to manually type all the info needed for setting up VPN on the phone. It is easy to make a mistake when typing, which can lead to severe headaches trying to trouble shoot the problem. If you have several accounts to set up, or several phones to configure, then it can become quite a project.
The following short guide shows how to use the import feature of the 1 VPN app to make it easier to set up VPN. The import function lets you import one or more VPN configurations from a file or email. This feature is also useful for backup and restore of your VPN settings. The 1 VPN app can be bought from the Android market or from the app developers website http://doandroids.com/Apps/OneVpn/.
Step 1.
Lets assume we are setting up a PPTP connection, then the first thing to do is to send the following text in an email from your computer to your Android phone:
PPTP; My VPN; username; password; vpn.myserver.com; ; true
Naturally you should replace 'username', 'password' and 'vpn.myserver.com' with something which is appropriate for your particular VPN server. Also, if you don't want encryption, you should replace 'true' above, with 'false'. You may also want to leave the password empty, for security reasons, in which case you will have to fill it in later when you connect to the VPN.
Step 2
Open the email you just sent on your Android phone using the Gmail client, and copy the text to the clip board. To copy text from an email you need to press "Menu>More>Select Text", then select the text to be copied by dragging your finger across it.
Step 3
Open the 1 VPN app, and click the "Add". On the following screen click "Import profiles" (the last option). This will bring up the Import Wizard seen below. Next you click "From Clip board", which imports your set-up and shows the list of profiles just inported (in this case only 'My VPN'). Click OK and you're done.
More
The 1 VPN app makes it easier to use VPN on Android, with the import function being one of its handy features. There is also an export function which lets you backup your VPN set-ups, or copy it to other phones.
To learn more about the import/export features of 1 VPN on Android, you can look at: http://doandroids.com/Apps/OneVpn/how-to/import/.
The main time saver of 1 VPN is its unique 1-click connect feature. To learn more about 1 VPN visit http://doandroids.com/Apps/OneVpn/faq/.
Posted by android 1
0 CommentsIntentService - handy but missing a constructor
2010-07-11 06:16:50
The IntentService class is handy indeed. It's a service that spawns a separate thread for the work it does. All you need to do to use it is to extend it, override the onHandleIntent method, and then call startService to invoke it. E.g. something like this:
public class MyIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
// do some work
}
}
public class MyActivity extends Activity {
...
void startMyService() {
Intent i = new Intent(this, MyActivity.class);
// The work will be done in a separate thread, so it doesn't block
// this activity
startService(i);
}
}
At least that's what the current doc about IntentService says. There's one more important thing to know though: the MyIntentService has to a default constructor, that takes no arguments, e.g. as so:
MyIntentService() {
super("MyIntentServiceName");
}
The system expects there to be such a constructor, and if it is missing you will get a InstantiationException when startService is called (if there's no constructor at all you will get a compilation error).
The string passed to super above is arbitrary, and I can't really say I know what it is used for. It is needed since the only constructor exposed by IntentService takes a String argument.
Enjoy using your IntentServices.
Posted by android 2
0 CommentsAndroid Drag and Drop
2010-06-25 19:54:17
A fundamental UI feature such as Drag and Drop should be trivial to implement, right? In fact, on Android it isn't that difficult (depending on how flexible you wanna be of course). That said, it does require that you implement your own view class(es) so if that turns you off, then you'd better stop reading now :)
Most drag and drop references I've found online refer to the source code of the Android Media Player. It's TouchInterceptor class implements a drag and drop of elements in a list. Another resource, which provides a more generic, but also more complicated, drag and drop is the Android Launcher app.
Since I wanted more than just a list rearrangement, I decided to follow the example of the Launcher, albeit much much simpler. The basic idea is to introduce a class, called DragLayer, that extends FrameLayout and which takes care of most of the actual dragging. Unlike in the Launcher, however, I would restrict the DragLayer to only moving the object around, and let whatever user of the layer take care of things like scrolling, dropping it in place, et c. Also, the drag layer of the Launcher creates a copy of the view that is to be moved, rather than moving the actual view. This is obviously useful in many cases, but not always necessary, so I decided to make that optional.
A key in implementing the DragLayer class is understanding the interaction between the touch detection callbacks that exists in a ViewGroup. They are:
-
boolean onTouchEvent(MotionEvent ev)- called whenever a touch event with thisViewas target is detected -
boolean onInterceptTouchEvent(MotionEvent ev)- called whenever a touch event is detected with thisViewGroupor a child of it as target. If this function returnstrue, theMotionEventwill be intercepted, meaning it will be not be passed on to the child, but rather to theonTouchEventof thisView.
So in order to for our DragLayer to take charge of the drag movements, it needs to implement these two functions. In addition I let it implement two functions startDragging and stopDragging as given in the below outline. This example only handles movement on the x-axis, but that's should be very straight forward to extend:
class DragLayer extends FrameLayout {
...
// Called by user with a child of this DragLayer to be
// dragged
public void startDragging(View child) {
// Do initial setup to enable moving the child
mStartX=mLastMotionX;
mStartRightMargin = getWidth()-child.getRight();
mDragView=child;
doDrag(mStartX);
// Indicate that we are now in dragging mode.
mIsDragging = true;
}
// Moves the object by changing the right margin
void doDrag(int x) {
View v=mDragView;
LayoutParams params = new LayoutParams(v.getLayoutParams());
params.gravity = Gravity.RIGHT;
params.rightMargin = (int) (mStartX-x) + mStartRightMargin;
v.setLayoutParams(params);
}
// Called internally when dragging is stopped
protected void stopDragging(int x) {
if (mIsDragging) {
mIsDragging = false;
// Call back to user
callStopCallback(x);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
final float x = ev.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Remember each down event,
// as it is needed in startDragging above
mLastMotionX = x;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// User stopped dragging, or some other view
// has taken over the motion event
// Normally this is handled by onTouchEvent below,
// but it seems safest to also handle it here
stopDragging(x);
break;
}
// Whilst dragging, we return true in order to dispatch
// the MotionEvent to our onTouchEvent method below.
return mIsDragging;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mIsDragging) {
// Not in dragging mode, so no action taken
return super.onTouchEvent(ev);
}
final int action = ev.getAction();
float x = ev.getX();
switch (action) {
case MotionEvent.ACTION_MOVE:
// Move the object.
doDrag(x);
// Call back to user
callMoveCallback(x);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
stopDragging(x);
break;
}
return true;
}
...
}
A basic user of this class could be an activity with the following layout:
<com.example.dragndrop.DragLayer ...>
<TextView android:text="A" ...>
<TextView android:text="B" ...>
<TextView android:text="C" ...>
</com.example.dragndrop.DragLayer>
When the user clicks on any of the TextView children, the activity would call startDragging which would let the user move the view around.
As mentioned, the above is rough outline, just to illustrate the ideas of the basic machinery. That said, a more complete solution won't be much more complex.
If you want to see an example of a more complete DragLayer in action you can check out our app Amusing Snippets. Its layout includes a tool bar, which is a DragLayer containing a list of icons. An icon in the tool bar can be moved around inside the tool bar after long pressing it.
Posted by android 1
0 CommentsAndroid ClassLoader - dynamic loading of class by name
2010-06-10 04:32:03
So you have the names of a bunch of classes, how can you load them? Here's the code (needs to be in an activity to make use of getPackageManager()):
String packageName = "com.example.mypackage";
String className = "com.example.mypackage.MyClass";
String apkName = getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
PathClassLoader myClassLoader =
new dalvik.system.PathClassLoader(
apkName,
ClassLoader.getSystemClassLoader());
Class<?> handler = Class.forName(className, true, myClassLoader);
You can combine several apk-files in the string apkName by separating them with a ":". Pretty simple, huh :)
Thanks to Josh for his blog post on Dynamic Loading. His example has a problem though, as he assumes the apk-file is located in /data/app/PACKAGENAME.apk. That may or may not be true (it wasn't for me), so better to use the PackageManager (as above).
Posted by android 2
0 CommentsPage 1 | Next Page