我想在通知中显示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));
}
}
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类
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的方法吗?