我创建了一个Android Widget。 我有一个问题,就是将活动的结果返回给它的AppWidgetProvider父级。 我在网上找了一个答案,确切地写了所有网站上的内容并且它没有用。
我在onReceive方法中设置了一个断点,它甚至没有进入那里。
如果有人可以帮助我,我会很高兴。 附件是我的AppWidgetProvider代码和名为ContactsActivity的活动。 也附在清单文件中。
的AppWidgetProvider:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent informationButtonIntent = new Intent(context, InfoActivity.class);
informationButtonIntent.setAction(INFO_BUTTON_PRESSED);
PendingIntent infoPendingIntent = PendingIntent.getActivity(context, 0, informationButtonIntent, 0);
Intent contactButtonIntent = new Intent(context, ContactsActivity.class);
contactButtonIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
contactButtonIntent.setAction(CONTACT_BUTTON_PRESSED);
PendingIntent contactPendingIntent = PendingIntent.getActivity(context, 0, contactButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent freeNumberButtonIntent = new Intent(context, FreeSMSNumberActivity.class);
contactButtonIntent.setAction(FREE_NUMBER_BUTTON_PRESSED);
PendingIntent freeNumberPendingIntent = PendingIntent.getActivity(context, 0, freeNumberButtonIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.informationButton, infoPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.contactsButton, contactPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.freeTextButton, freeNumberPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
}
else if (intent.getAction().equals("CONTACT_BUTTON_PRESSED")) {
}
super.onReceive(context, intent);
}
}
ContactsActivity:
public class ContactsActivity extends Activity {
private static final int PICK_CONTACT = 3;
int mAppWidgetId;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(UndisclosedSMS.TAG_LOGGER, "Contacts button was pressed");
Intent parentIntent = getIntent();
Bundle extras = parentIntent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK){
}
else {
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_CANCELED, resultValue);
finish();
}
finish();
}
}
finish();
}
}
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.LogiaGroup.UndisclosedSMS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<receiver android:name=".UndisclosedSMS" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/undisclose_sms_info" />
</receiver>
<activity android:name=".InfoActivity" >
<intent-filter >
<action android:name="com.LogiaGroup.UndisclosedSMS.INFO_BUTTON_PRESSED" />
</intent-filter>
</activity>
<activity android:name=".ContactsActivity" >
<intent-filter >
<action android:name="com.LogiaGroup.UndisclosedSMS.CONTACT_BUTTON_PRESSED" />
</intent-filter>
</activity>
<activity android:name=".FreeSMSNumberActivity" android:theme="@android:style/Theme.Dialog"
android:windowSoftInputMode="stateVisible|adjustResize">
<intent-filter >
<action android:name="com.LogiaGroup.UndisclosedSMS.FREE_NUMBER_BUTTON_PRESSED" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
非常感谢!