下面的代码应该描述一个应用程序,一旦点击了widget按钮,它就会发送一个应该由TestReceiver接收的意图。但是,在运行我的下面的代码时,永远不会调用TestReceiver的onReceive。
有人能让我知道我做错了吗?
小工具代码
public class Widget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
//Intent intent = new Intent(context.getApplicationContext(), TestReceiver.class);
Intent intent = new Intent();
intent.setAction(TestReceiver.TEST_INTENT);
intent.setClassName(TestReceiver.class.getPackage().getName(), TestReceiver.class.getName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.main);
views.setOnClickPendingIntent(R.id.btnTest, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
接收人代码:
public class TestReceiver extends BroadcastReceiver {
public static final String TEST_INTENT= "MyTestIntent";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Test", Toast.LENGTH_SHORT);
if(intent.getAction()==TEST_INTENT)
{
System.out.println("GOT THE INTENT");
Toast.makeText(context, "Test", Toast.LENGTH_SHORT);
}
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.intenttest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".TestReceiver" android:label="@string/app_name">
<intent-filter>
<action android:name="MyTestIntent">
</action>
</intent-filter>
</receiver>
<receiver android:label="@string/app_name" android:name="Widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
答案 0 :(得分:7)
它可能有效,但您忘了在Toast结束时添加.show()
:)
答案 1 :(得分:0)
==测试引用相等性(它们是否是同一个对象)。
.equals()测试值的相等性(它们是否在逻辑上&#34;相等&#34;)。
使用&#39; ==&#39;比较字符串值。不等于&#39;
这&#34; Toast.makeText(context, "Test", Toast.LENGTH_SHORT).show();
&#34;更改此&#34; package *********;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class TestReceiver extends BroadcastReceiver {
public static final String TEST_INTENT= "MyTestIntent";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Test holaaa", Toast.LENGTH_SHORT).show();
if(intent.getAction() == TEST_INTENT)
// if(intent.getAction().equals(TEST_INTENT))
{
System.out.println("GOT THE INTENT");
Toast.makeText(context, "Test Goooo", Toast.LENGTH_SHORT).show();
}
}
}
&#34;
当然还有{{1}}
所有代码:
{{1}}