5

I want to display a GridView inside a notification, I'm using a RemoteViews to display a custom layout. My code looks like:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showNotification();
    }

    private void showNotification(){

        RemoteViews rw = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
        rw.setRemoteAdapter(R.id.grid_view, new Intent(this, AppService.class));
        rw.setImageViewResource(R.id.image, R.mipmap.ic_launcher);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Custom View")
                .setContent(rw);    
        ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(1, builder.build());    

        //startService(new Intent(this, AppService.class));

    }

}

notification_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="64dp"
    android:verticalSpacing="0dp"
    android:columnWidth="192dp"
    android:numColumns="auto_fit"
    />
</LinearLayout>

AppService class

public class AppService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new AppRemoteViewFactory(this);
    }    
}

My Service is declared in the manifest like this

<service android:name=".AppService"
            android:permission="android.permission.BIND_REMOTEVIEWS"
            android:exported="false" />

Method onGetViewFactory from AppService is never called. What am I doing wrong? Is there another way to populate GridView?

aloj
  • 3,194
  • 7
  • 27
  • 38

0 Answers0