我在我的活动中启动了一个VPNService,我想在服务启动后启动一些线程。这就是我开始服务的方式:
Intent intent = VpnService.prepare(getActivity());
if (intent != null) {
startActivityForResult(intent, 123);
else {
onActivityResult(123, Activity.RESULT_OK, null);
}
在onActivityResult中我开始我的线程
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123) {
// Start some thread
}
}
现在我希望能够从我的应用程序小部件中完成所有这些操作。我似乎只能在widget中使用startActivity,这是使用VPNService的正确方法。有什么方法可以从我的小部件实现这一点吗?也许来自startActivity的一些回调?
答案 0 :(得分:1)
对小部件没有太多经验,但建议采用这种方式:
@android:style/Theme.NoDisplay
); 以下是示例项目:
的AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexstarc.testapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="19"
android:targetSdkVersion="19"/>
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:allowBackup="true">
<activity
android:name=".MyActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="TestWidgetProvider"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- Custom actions -->
<action android:name="com.alexstarc.testapp.ACTION_BTN" />
<action android:name="com.alexstarc.testapp.ACTION_VPN_RESULT" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
</application>
</manifest>
appwidget_info.xml:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="180dp"
android:minHeight="40dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/appwidget"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:previewImage="@drawable/widget_preview"/>
appwidget.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:background="@drawable/appwidget_bg">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/launchVPN"
android:src="@android:drawable/ic_media_play"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/resultText"
style="@android:style/TextAppearance.DeviceDefault.Medium.Inverse"
android:layout_toRightOf="@id/launchVPN"
android:text="@string/test_text"
android:gravity="bottom"/>
</RelativeLayout>
MyActivity.java:
public class MyActivity extends Activity {
private static final int VPN_LAUNCH_CODE = 123;
public static String SHARED_PREFS_NAME = "shared_prefs";
public static String RESULT_VPN = "vpn_launch_result";
public static String ACTION_VPN_RESULT = "com.alexstarc.testapp.ACTION_VPN_RESULT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = VpnService.prepare(MyActivity.this);
if (intent != null) {
startActivityForResult(intent, VPN_LAUNCH_CODE);
} else {
onActivityResult(VPN_LAUNCH_CODE, Activity.RESULT_OK, null);
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123) {
getSharedPreferences(SHARED_PREFS_NAME, MODE_MULTI_PROCESS).edit().putString(RESULT_VPN, "VPN Result is " + resultCode).commit();
final Intent resultIntent = new Intent(ACTION_VPN_RESULT);
sendBroadcast(resultIntent);
finish();
}
}
}
TestWidgetProvider.java:
public final class TestWidgetProvider extends AppWidgetProvider {
private static final String ACTION_START_BTN = "com.alexstarc.testapp.ACTION_BTN";
// TODO: Use Shared Prefs for this
private static int sWidgetId = 0;
private static boolean sVPNDone = false;
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
updateWidget(context, appWidgetManager, widgetId);
}
sWidgetId = appWidgetIds[0];
}
/**
* Updates single App Widget
*
* @param context {@link android.content.Context}
* @param appWidgetManager {@link android.appwidget.AppWidgetManager}
* @param widgetId of current widget
*/
private static void updateWidget(final Context context, final AppWidgetManager appWidgetManager, final int widgetId) {
final ArrayList<Integer> widgets = new ArrayList<Integer>();
final Intent btnClickIntent = new Intent(context, TestWidgetProvider.class);
btnClickIntent.setAction(ACTION_START_BTN);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, btnClickIntent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setOnClickPendingIntent(R.id.launchVPN, pendingIntent);
// Update from shared preferences after activity was launched
if (sVPNDone) {
views.setTextViewText(R.id.resultText,
context.getSharedPreferences(MyActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE).getString(MyActivity.RESULT_VPN, "No result"));
}
appWidgetManager.updateAppWidget(widgetId, views);
}
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (ACTION_START_BTN.equals(action)) {
// Start listening service
final Intent vpnActivityIntent = new Intent(context, MyActivity.class);
vpnActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(vpnActivityIntent);
} else if (MyActivity.ACTION_VPN_RESULT.equals(action)) {
sVPNDone = true;
updateWidget(context, AppWidgetManager.getInstance(context), sWidgetId);
} else {
super.onReceive(context, intent);
}
}
}
答案 1 :(得分:0)
您希望对此类事物使用隐式意图。如果您只确保使用相同的密钥对软件包进行签名,您甚至可以使用此方案将小部件打包到另一个应用程序而不是服务中。
的AndroidManifest.xml:
<!-- Specify a new kind of permission based on your signature -->
<permission
android:name="com.example.VPN"
android:protectionLevel="signature" />
<!-- This application uses this new permission -->
<uses-permission android:name="com.example.VPN" />
<!-- Let the VPN service use the permission and listen for an implicit intent -->
<service
android:name="com.example.VpnService"
android:exported="true"
android:permission="com.example.VPN" >
<intent-filter>
<action android:name="com.example.intent.action.START_VPN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
VpnService:
public static final String ACTION_START_VPN = "com.example.intent.action.START_VPN";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (null != intent && ACTION_START_VPN.equals(intent.getAction()) {
// Do your stuff here
}
}
在你的小部件中:
Intent intent = new Intent(VpnService.ACTION_START_VPN);
context.startService(intent);