4

我想在通知中显示 GridView,我正在使用 RemoteViews 来显示自定义布局。我的代码如下所示:

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));

    }

}

通知布局.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>

应用服务类

public class AppService extends RemoteViewsService {

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

我的服务在清单中声明如下

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

AppService 中的 onGetViewFactory 方法永远不会被调用。我究竟做错了什么?还有另一种填充 GridView 的方法吗?

4

0 回答 0