2017-06-06 18 views
-2

請告訴我什麼,我做錯了如何通過單擊android studio中的主屏幕小部件來顯示敬酒消息?

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開發很陌生,所以不勝感激。

+0

是你的日誌,到處工作?也就是說,「Toast」是唯一不起作用的東西? –

+1

吐司評論你的代碼。 –

+0

@MikeM。 handleActionSend中的日誌不起作用 – kinjol

回答

0

因爲小部件延伸的AppWidgetProvider,這樣的類不能給你打電話不能顯示小窗口的點擊吐司當前上下文

如果你想顯示敬酒時創建控件或選擇小部件可以通過使用波紋管代碼:

Handler handler = new Handler(Looper.getMainLooper()); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(context,"hiii dhruv", Toast.LENGTH_LONG).show(); 
      } 
     }); 

======================================= ===================================

如何在您的widget類中使用以上代碼

@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(); 
      } 
     }); 

    } 

================================== ==================================

@Override 
    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); 
     Toast.makeText(context, "hiiiiiiiiiiii", Toast.LENGTH_SHORT).show(); 
    } 
+0

您提供的鏈接幫助了很多。我在onRecieve方法上做了重寫,現在它可以工作。謝謝 – kinjol

+0

@kinjol是的,你是對的,如果你喜歡我的代碼,那麼請正確標記它 謝謝 –

-1

請看到這一點:Android: Toast in a thread

如果麪包沒有顯示那麼它很可能不使用UI線程。你可以嘗試使用applicationContext而不是這個,儘管我不確定這會起作用。我不喜歡在用戶界面不可見時運行Toasts,但已經運行這個用於特定測試而無需查看日誌。