从 v4 升级项目后,找不到类“android.support.v7.app.NotificationCompat$Builder”
我正在尝试使用通知来管理我的 mediaplyaer 控件。
我需要为我的项目使用 android.support.v7.app.NotificationCompat,以便根据这个问题进一步处理锁屏控件。
https://stackoverflow.com/a/31013955/3126760
我已经将我的支持库更新到 23.0.1
当我尝试构建以下代码时
public class player6 extends Service implements OnCompletionListener,
OnPreparedListener, OnErrorListener, OnSeekCompleteListener,
OnInfoListener, OnBufferingUpdateListener {
final int NOTIFICATION_ID = 1;
public static final String EXTRA_AUDIO_URL = "audio_url";
public static final String BROADCAST_PLAYBACK_STOP = "stop",
BROADCAST_PLAYBACK_PAUSE = "pause",
BROADCAST_PLAYBACK_NEXT = "next",
BROADCAST_PLAYBACK_PREVIOUS = "previous",
BROADCAST_PLAYBACK_SHUFFLE = "shuffle",
BROADCAST_PLAYBACK_REPEAT = "repeat";
int song_aLBUM;
private boolean isShuffle = false;
private boolean isRepeat = false;
ArrayList<String> songs;
Cursor cursor;
Uri uri;
Bundle b;
private int index;
Button play, next, back, eqaulize;
ArrayList<String> MultilistArray;
MediaPlayer mediaPlayer;
String url;
String x;
// seekbar variables for processing
String sntSeekPos;
int intSeekPos;
int mediaPosition;
int mediaMax;
private final Handler handler = new Handler();
private static int songEnded;
public static final String BROADCAST_ACTION = "com.example.classanimate.seekprogress";
Intent bufferIntent;
Intent seekIntent;
//lolipop
private MediaSessionManager mManager;
private MediaSession mSession;
private MediaController mController;
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BROADCAST_PLAYBACK_STOP))
stopSelf();
else if (action.equals(BROADCAST_PLAYBACK_PAUSE)) {
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
else
mediaPlayer.start();
} else if (action.equals(BROADCAST_PLAYBACK_NEXT)) {
playnext();
} else if (action.equals(BROADCAST_PLAYBACK_PREVIOUS)) {
playprevious();
} else if (action.equals(BROADCAST_PLAYBACK_SHUFFLE)) {
playShuffle();
} else if (action.equals(BROADCAST_PLAYBACK_REPEAT)) {
playRepeat();
} else if (action.equals(playeractivity.BROADCAST_SEEKBAR)) {
updateSeekPos(intent);
}
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
b = intent.getExtras();
index = b.getInt("position");
MultilistArray = intent.getStringArrayListExtra("multilist");
getMusic();
playSong(index);
showNotification();
setupHandler();
return START_STICKY;
}
@Override
public void onCreate() {
seekIntent = new Intent(BROADCAST_ACTION);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnSeekCompleteListener(this);
mediaPlayer.setOnInfoListener(this);
mediaPlayer.setOnBufferingUpdateListener(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BROADCAST_PLAYBACK_STOP);
intentFilter.addAction(BROADCAST_PLAYBACK_PAUSE);
intentFilter.addAction(BROADCAST_PLAYBACK_NEXT);
intentFilter.addAction(BROADCAST_PLAYBACK_PREVIOUS);
intentFilter.addAction(BROADCAST_PLAYBACK_SHUFFLE);
intentFilter.addAction(BROADCAST_PLAYBACK_REPEAT);
intentFilter.addAction(playeractivity.BROADCAST_SEEKBAR);
// LocalBroadcastManager.getInstance(this).
registerReceiver(broadcastReceiver, intentFilter);
}
public void getMusic() {
// TODO Auto-generated method stub
for (Iterator iterator = MultilistArray.iterator(); iterator.hasNext();) {
x = (String) iterator.next();
// do some stuff
}
uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String projection[] = { android.provider.MediaStore.Audio.Media.DATA,
android.provider.MediaStore.Audio.Media.TITLE,
android.provider.MediaStore.Audio.Media.ARTIST,
android.provider.MediaStore.Audio.Media.ALBUM,
android.provider.MediaStore.Audio.Media.COMPOSER,
android.provider.MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media._ID,
// android.provider.MediaStore.Audio.Albums.ALBUM_ART,
android.provider.MediaStore.Audio.Media.ALBUM_ID };
String selection1 = MediaStore.Audio.Media._ID + " IN(";
// String[] selectionArgs = "{";
for (int i = 0; i < MultilistArray.size(); i++) {
selection1 += "?, ";
}
selection1 = selection1.substring(0, selection1.length() - 2) + ")";
String[] selectionArgs = new String[MultilistArray.size()];
selectionArgs = MultilistArray.toArray(selectionArgs);
cursor = this.getContentResolver().query(uri, projection, selection1,
selectionArgs, null);
songs = new ArrayList<String>();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
songs.add(cursor.getString(2));
cursor.moveToNext();
}
}
public void playSong(int index2) {
// public void playSong() {
if (b.getInt("position") != 0) {
cursor.moveToPosition(0);
}
else{
cursor.moveToPosition(index);
}
int song_id = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
String song_name = cursor.getString(cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA));
song_aLBUM = cursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
String song_album_name = cursor.getString(song_aLBUM);
SongDetailsId();
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(song_name);
mediaPlayer.prepare();
// sb.setMax(mediaplayer.getDuration());
mediaPlayer.start();
// int totalDuration = mediaplayer.getDuration();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onCompletion(MediaPlayer mp) {
// stopSelf();
if (isRepeat) {
// repeat is on play same song again
playSong(index);
} else if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
index = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
playSong(index);
} else {
// no repeat or shuffle ON - play next song
if (index < (songs.size() - 1)) {
playSong(index + 1);
index = index + 1;
} else {
// play first song
playSong(0);
index = 0;
}
}
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("Service", "MediaPlayer prepared. Music will play now.");
mediaPlayer.start();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
private PendingIntent makePendingIntent(String broadcast) {
Intent intent = new Intent(broadcast);
return PendingIntent
.getBroadcast(getApplicationContext(), 0, intent, 0);
}
private void showNotification() {
// Create notification
Builder notificationBuilder = (Builder) new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(this.url)
// audio url will show in notification
.setContentIntent(
PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(),
player6.class), 0))
.addAction(R.drawable.ic_action_play, "Stop",
makePendingIntent(BROADCAST_PLAYBACK_STOP))
.addAction(R.drawable.ic_action_pause, "Pause",
makePendingIntent(BROADCAST_PLAYBACK_PAUSE))
.addAction(R.drawable.img_btn_forward, "next",
makePendingIntent(BROADCAST_PLAYBACK_NEXT));
// Show notification
startForeground(NOTIFICATION_ID, notificationBuilder.build());
}
private void playnext() {
// TODO Auto-generated method stub
if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
index = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
playSong(index);
} else {
if (index < (songs.size() - 1)) {
index += 1;
// sb.setMax(mediaplayer.getDuration());
playSong(index);
} else {
index = 0;
playSong(index);
}
}
}
private void playprevious() {
// TODO Auto-generated method stub
if (isShuffle) {
// shuffle is on - play a random song
Random rand = new Random();
index = rand.nextInt((songs.size() - 1) - 0 + 1) + 0;
playSong(index);
} else {
if (index > 0) {
index -= 1;
playSong(index);
} else {
index = songs.size() - 1;
playSong(index);
}
}
}
private void playShuffle() {
// TODO Auto-generated method stub
Toast.makeText(this, "playShuffle", Toast.LENGTH_SHORT).show();
if (isShuffle) {
isShuffle = false;
playeractivity.shuffle.setText("Shuffle is OFF");
} else {
// make repeat to true
isShuffle = true;
// make shuffle to false
isRepeat = false;
playeractivity.shuffle.setText("Shuffle is ON");
playeractivity.repeat.setText("Repeat is OFF");
}
}
private void playRepeat() {
// TODO Auto-generated method stub
Toast.makeText(this, "btrepeat", Toast.LENGTH_SHORT).show();
if (isRepeat) {
isRepeat = false;
playeractivity.repeat.setText("Repeat is OFF");
} else {
// make repeat to true
isRepeat = true;
isShuffle = false;
playeractivity.repeat.setText("Repeat is ON");
playeractivity.shuffle.setText("Shuffle is OFF");
// btnShuffle.setImageResource(R.drawable.btn_shuffle);
}
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onSeekComplete(MediaPlayer mp) {
// TODO Auto-generated method stub
if (!mediaPlayer.isPlaying()) {
playSong(index);
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
private void setupHandler() {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 500);
}
private Runnable sendUpdatesToUI = new Runnable() {
@Override
public void run() {
LogMediaPosition();
handler.postDelayed(this, 500);
}
};
private void LogMediaPosition() {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying()) {
mediaPosition = mediaPlayer.getCurrentPosition();
mediaMax = mediaPlayer.getDuration();
seekIntent.putExtra("counter", String.valueOf(mediaPosition));
seekIntent.putExtra("mediamax", String.valueOf(mediaMax));
seekIntent.putExtra("song_ended", String.valueOf(songEnded));
sendBroadcast(seekIntent);
}
}
public void updateSeekPos(Intent intent) {
int seekPos = intent.getIntExtra("seekpos", 0);
if (mediaPlayer.isPlaying()) {
handler.removeCallbacks(sendUpdatesToUI);
// Toast.makeText(this, "positon" + seekPos,
// Toast.LENGTH_SHORT).show();
mediaPlayer.seekTo(seekPos);
setupHandler();
}
}
@Override
public void onDestroy() // called when the service is stopped
{
mediaPlayer.stop();
stopForeground(true);
handler.removeCallbacks(sendUpdatesToUI);
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
public void SongDetailsId() {
Intent intent = new Intent(playeractivity.SONG_DETAILS);
intent.putExtra("value", index);
sendBroadcast(intent);
}
}
我在日志 cat 中收到以下错误
10-16 10:07:02.742: E/dalvikvm(1134): 找不到类“android.support.v7.app.NotificationCompat$Builder”,从方法 com.example.multiplayer.player6 中引用.showNotification
这些是我的进口
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v7.app.NotificationCompat;
import android.support.v7.app.NotificationCompat.Builder;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
现在我知道如果我使用 import android.support.v4.app.NotificationCompat; 它会解决我当前的问题,但我需要使用 import android.support.v7。 app.NotificationCompat 用于我 future 的工作。
问题发生在
private void showNotification() {
// Create notification
Builder notificationBuilder = (Builder) new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(this.url)
// audio url will show in notification
.setContentIntent(
PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(),
player6.class), 0))
.addAction(R.drawable.ic_action_play, "Stop",
makePendingIntent(BROADCAST_PLAYBACK_STOP))
.addAction(R.drawable.ic_action_pause, "Pause",
makePendingIntent(BROADCAST_PLAYBACK_PAUSE))
.addAction(R.drawable.img_btn_forward, "next",
makePendingIntent(BROADCAST_PLAYBACK_NEXT));
// Show notification
startForeground(NOTIFICATION_ID, notificationBuilder.build());
}
我尝试了很多东西,包括使用 android.support.v4.app.NotificationCompat.Builder notificationBuilder =
还尝试添加类型转换(BULIDER),但每次我都会遇到同样的错误。
如何将 android.support.v7.app.NotificationCompat 与 Builder 一起使用
最佳答案
这对我有帮助
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_ic_notification);
等等。不知道为什么必须这样格式化,但它对我有用。
关于java - 从 v4 升级项目后找不到类 'android.support.v7.app.NotificationCompat$Builder',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33163049/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("