How to create android app with app widget in single application

前端 未结 2 1108
终归单人心
终归单人心 2020-12-02 08:13

I have already done one android application which stores date with person name and phone number.

Now I have to develop one widget f

2条回答
  •  一向
    一向 (楼主)
    2020-12-02 09:12

    I am finding couple of days I got somehow the solution. So I am sharing my idea.

    This site has been very useful for me.

    http://kasperholtze.com/android/how-to-make-a-simple-android-widget/

    I made some changes and did my work.

    Requirement solution:

    1. Make Receiver and put into manifest permission like this.

    
            
                
            
            
        
    

    2. In my side require top TextView click so can make using TextView id in onUpdate().

        remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_lay);
    
        Intent intent = new Intent(context, TestActivity.class);
        intent.setAction("callActivity");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent); 
        appWidgetManager.updateAppWidget(new ComponentName(context,WatchWidget.class), remoteViews);
    

    3. I want to call database and fetch record and display it. Now question what about update?

    So I have used timer class and run every 1000sec because of over widget is always update 30min(may be).

    @Override
    public void onUpdate( final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds )
    {
    
        this.appWidgetManager = appWidgetManager;
        try {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager,appWidgetIds), 1, 1000);
    
        } catch (Exception e) {
            System.out.println("Exception:");
        }
        updateSwitch1(context, appWidgetManager, appWidgetIds[0]);
    
    }
    

    If any have queries then put a comment and thanks to all who gave me full support to make this answer useful.

提交回复
热议问题