1.根据图片名字获取图片id
/**
* 根据图片名字获取图片id
* @param imageName
* @param context
* @return
*/
public static int getResource(String imageName, Context context) {
int resId = context.getResources()
.getIdentifier(imageName, "drawable", context.getPackageName());
return resId;
}
2.根据名字从Assets中读取文件,以图片为例子
/**
* 根据名字从Assets中读取图片
* @param fileName
* @return
*/
public static Bitmap getImageFromAssetsFile(String fileName,Context context )
{
Bitmap image = null;
AssetManager am =context.getResources().getAssets();
try
{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
3.在service中的BroadcasReceiver遇到no empty constructor错误
内部的broadcastReceiver需要被动态注册,或者是static类。
这样就OK了
private void registerReceiver(){
xxxReceiver = new �xxxReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("xxxx");
}
或者
public static class MusicReceiver extends BroadcastReceiver {
public MusicReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getIntExtra(STATUS, 0)) {
case PREVIOUS:
Log.e(TAG, "onReceive:前一首 " );
break;
case PAUSE:
Log.e(TAG, "onReceive:暂停/播放 " );
break;
case NEXT:
Log.e(TAG, "onReceive:下一首 " );
break;
}
}
}
然后:
<receiver android:name=".Service.MusicService$MusicReceiver">
<intent-filter>
<action android:name="com.example.yungui.music.UpdateStatus" />
</intent-filter>
</receiver>
网友评论