问题描述
在这里完成android开发新手,予以警告。
Complete android development newbie here, be warned.
我试图建立一个主屏幕小工具,让您挖掘插件调用predefined数量。不过,我可以创建窗口小部件并将其添加到主屏幕就好了,当我试图让小部件可点击使用setOnClickPendingIntent的,它不会启动该活动。在code我现在用的就是提供如下:
I'm trying to develop a homescreen widget that allows you to tap the widget to call a predefined number. I can create the widget and add it to the homescreen just fine, however, when I try to make the widget clickable using setOnClickPendingIntent, it does not launch the activity. The code I am using is provided below:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.callwidget_layout);
Log.d("stuff", "remoteview defined");
Intent callIntent = new Intent(Intent.ACTION_CALL);
Log.d("stuff", "intent created");
callIntent.setData(Uri.parse("tel:"+8888888));
Log.d("stuff", "intent data added");
PendingIntent clickPI=PendingIntent
.getBroadcast(context, 0,
callIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("stuff", "pending intent created");
remoteViews.setOnClickPendingIntent(R.layout.callwidget_layout, clickPI);
Log.d("stuff", "setonclickpendingintent created");
}
在Log.d方法做工精细,当他们出现在logcat的输出,但攻小部件不执行任何操作。有迹象表明,显示在logcat中没有其他的错误消息。难道有什么我做错了吗?
The Log.d methods work fine, as they show up on the logcat output, but tapping the widget does nothing. There are no other error messages that show up on logcat. Is there anything I am doing wrong?
更新:改变了setOnClickPendingIntent引用一个按钮,ID为callbutton( remoteViews.setOnClickPendingIntent(R.id.callbutton,clickPI);
),并且还试图加入这三行code到的OnUpdate方法:
UPDATE: Changed the setOnClickPendingIntent to reference a button with the ID "callbutton" (remoteViews.setOnClickPendingIntent(R.id.callbutton, clickPI);
), and also tried adding these three lines of code to the onUpdate method:
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.getInstance(context).updateAppWidget(myWidget, remoteViews);
Log.d("stuff", "widget updated");
同样,Log.d方法的工作原理,这表明小部件更新罚款,但轻点按钮仍然不能做任何事情。
Again, the Log.d method works, suggesting that the widgets update fine, but tapping the button still does not do anything.
更新2:修改 PendingIntent clickPI = PendingIntent.getBroadcast
到 PendingIntent clickPI = PendingIntent.getActivity
并没有任何做任何事情。
UPDATE 2: Changing PendingIntent clickPI=PendingIntent.getBroadcast
to PendingIntent clickPI=PendingIntent.getActivity
does not do anything either.
推荐答案
好了,所以我都有点一种变通方法这个问题将不得不这样做,直到我找到一个实际的解决方案。这涉及到发射,这反过来启动我想要的目的的活动。我最初想避免这种情况,但哦。
Alright, so I have a bit of a workaround to this problem that will have to do until I find an actual solution. This involves launching an activity that in turn launches the intent I want. I initially wanted to avoid this, but oh well.
我的新code,任何人都可能需要它:
My new code, for anyone that might need it:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.callwidget_layout);
Log.d("stuff", "remoteview defined");
Intent callIntent = new Intent(context, CallActivity.class);
PendingIntent clickPI=PendingIntent
.getActivity(context, 0,
callIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d("stuff", "pending intent created");
remoteViews.setOnClickPendingIntent(R.id.callbutton, clickPI);
Log.d("stuff", "setonclickpendingintent created");
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.getInstance(context).updateAppWidget(myWidget, remoteViews);
Log.d("stuff", "widget updated");
}
CallActivity.java:
CallActivity.java:
package com.example.callzepolice;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class CallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
Intent callIntent = new Intent(Intent.ACTION_CALL);
Log.d("stuff", "intent created");
callIntent.setData(Uri.parse("tel:"+8888888));
Log.d("stuff", "intent data added");
startActivity(callIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.call, menu);
return true;
}
}
这篇关于窗口小部件和setOnClickPendingIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!