Tricks to start VPN service in background
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@
|
||||
*/
|
||||
package com.github.shadowsocks
|
||||
|
||||
import android.app.{NotificationManager, Notification, PendingIntent, Service}
|
||||
import android.app._
|
||||
import android.content._
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
@@ -166,10 +166,15 @@ class ShadowVpnService extends VpnService {
|
||||
}
|
||||
|
||||
def handleCommand(intent: Intent) {
|
||||
if (intent == null) {
|
||||
|
||||
if (VpnService.prepare(this) != null) {
|
||||
val i = new Intent(this, classOf[ShadowVpnActivity])
|
||||
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
startActivity(i)
|
||||
stopSelf()
|
||||
return
|
||||
}
|
||||
|
||||
appHost = settings.getString("proxy", "127.0.0.1")
|
||||
sitekey = settings.getString("sitekey", "default")
|
||||
encMethod = settings.getString("encMethod", "table")
|
||||
@@ -362,7 +367,6 @@ class ShadowVpnService extends VpnService {
|
||||
super.onCreate()
|
||||
EasyTracker.getTracker.sendEvent("service", "start", getVersionName, 0L)
|
||||
settings = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
|
||||
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
|
||||
.asInstanceOf[NotificationManager]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user