Tricks to start VPN service in background

This commit is contained in:
Max Lv
2013-05-16 14:42:02 +08:00
parent 18e62360d8
commit 2eb0b193ca
3 changed files with 52 additions and 9 deletions
@@ -0,0 +1,33 @@
package com.github.shadowsocks
import android.app.Activity
import android.os.Bundle
import android.net.VpnService
import android.content.Intent
import android.util.Log
class ShadowVpnActivity extends Activity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
val intent = VpnService.prepare(this)
if (intent != null) {
startActivityForResult(intent, Shadowsocks.REQUEST_CONNECT)
} else {
onActivityResult(Shadowsocks.REQUEST_CONNECT, Activity.RESULT_OK, null)
}
}
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
resultCode match {
case Activity.RESULT_OK => {
val it: Intent = new Intent(this, classOf[ShadowVpnService])
startService(it)
}
case _ => {
Log.e(Shadowsocks.TAG, "Failed to start VpnService")
}
}
finish()
}
}