我是Android开发的新手。而且我一直在寻找过去一周的解决方案。我无法找到解决方案。
我正在构建一个运行计时器的小部件。我想在特定时间执行类似startActivity(intent)的操作或者调整android.app.Activity。我试图在这里测试的是每隔30秒使用Intent.Action_call调用一个数字我的道歉是不好的代码。我还处于学习阶段。
到目前为止我所尝试的是
public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.i("MyActivity", Integer.toString(appWidgetIds.length ));
Log.i("MyActivity", "onUpdate(): ");
for (int appWidgetId : appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new timertick(context, appWidgetManager), 1, 1000);
}
}
}
class timertick extends TimerTask {
public timertick(Context context, AppWidgetManager appWidgetManager) {
cont = context;
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, widget.class);
count++;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
Date dt= new Date();
remoteViews.setTextViewText(R.id.textView1,
"Time = " + format.format(dt));
Log.i("MyActivity", Integer.toString(count));
if (dt.getSeconds() % 30== 0) {// here if can be any conditions like dt="some day and time"
Log.i("MyActivity", "entering");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
// I want to do some thing like this startActivity(callIntent) unfortunately we cannnot use it with widget provider.
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
期待您的投入。请建议我如何实现这一目标。
答案 0 :(得分:1)
您可以使用在构造函数中收到的上下文来调用startActivity:
cont.startActivity(callIntent);