Java 如何在android studio中单击主屏幕小部件来显示祝酒词?
请告诉我我做错了什么Java 如何在android studio中单击主屏幕小部件来显示祝酒词?,java,android,Java,Android,请告诉我我做错了什么 NewAppWidget.java class package com.example.kinjolnath.alert; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content
NewAppWidget.java class
package com.example.kinjolnath.alert;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
private static final String TAG = NewAppWidget.class.getSimpleName();
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Log.d(TAG, "Inside updateAppWidget");
Log.i(TAG, "Inside updateAppWidget");
Log.e(TAG, "Inside updateAppWidget");
// Toast.makeText(context,
// "Should appear at widget click", Toast.LENGTH_SHORT).show();
// Construct the RemoteViews object
Intent intent = new Intent(context, MapsActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
Intent sendingIntent = new Intent(context, SendingService.class);
sendingIntent.setAction(SendingService.ACTION_SEND_MESSAGE);
PendingIntent sendingPendingIntent = PendingIntent.getService(context, 0, sendingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageButton, sendingPendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
Log.d(TAG, "Inside onUpdate");
Log.i(TAG, "Inside onUpdate");
Log.e(TAG, "Inside onUpdate");
SendingService.startActionSendMessage(context);
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
Log.d(TAG, "Inside onEnabled");
Log.i(TAG, "Inside onEnabled");
Log.e(TAG, "Inside onEnabled");
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
Log.d(TAG, "Inside onDisabled");
Log.i(TAG, "Inside onDisabled");
Log.e(TAG, "Inside onDisabled");
}
}
SendingService.java类
package com.example.kinjolnath.alert;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
/**
* Created by kinjolnath on 01-06-2017.
*/
public class SendingService extends IntentService{
private static final String TAG = NewAppWidget.class.getSimpleName();
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public static final String ACTION_SEND_MESSAGE = "com.example.MapsActivity.action.send_message";
public SendingService(String name) {
super(name);
}
public static void startActionSendMessage(Context context){
Intent intent = new Intent(context, SendingService.class);
intent.setAction(ACTION_SEND_MESSAGE);
context.startService(intent);
Log.d(TAG, "Inside startActionSendMessage");
Log.i(TAG, "Inside startActionSendMessage");
Log.e(TAG, "Inside startActionSendMessage");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null){
final String action = intent.getAction();
Log.d(TAG, action + "inside onHandleIntent");
if(ACTION_SEND_MESSAGE.equals(action)){
handleActionSend();
}
}
}
//handleActionSend method:
private void handleActionSend() {
Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Inside handleActionSend");
Log.i(TAG, "Inside handleActionSend");
Log.e(TAG, "Inside handleActionSend");
}
}
我遵循了Udacity的教程,并在这个阶段结束,但它仍然不起作用
我是android开发新手,希望您能给予我帮助。请看以下内容:
如果toast没有显示,那么它可能没有使用UI线程。您可以尝试使用applicationContext来代替它,尽管我不确定这是否有效。我不喜欢在UI不可见时运行Toast,但我已经运行了Toast以用于特定测试,而不必查看日志。您不能在单击小部件时显示Toast,因为小部件扩展了AppWidgetProvider,因此类无法调用当前上下文 如果您想在可以使用以下代码创建小部件或选择小部件时显示Toast:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"hiii dhruv", Toast.LENGTH_LONG).show();
}
});
==========================================================================
如何在小部件类中使用上述代码
@Override
public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"hiii dhruv", Toast.LENGTH_LONG).show();
}
});
}
====================================================================
或
你的日志到处都有用吗?也就是说,
Toast
是唯一不起作用的东西吗?Toast会为您的代码添加注释。@MikeM。handleActionSend中的日志不可用working@santoshkumar敬酒词在handleActionSend方法中。您提供的链接帮助很大。我在OnReceive方法上做了一个重写,现在它可以工作了。thanks@kinjol是的,你是对的,如果你喜欢我的代码,那么请在上面做正确的标记。谢谢
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, "hiiiiiiiiiiii", Toast.LENGTH_SHORT).show();
}