Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到类';android.support.v7.app.NotificationCompat$Builder';,从v4升级项目后_Java_Android_Notifications_Builder_Android Appcompat - Fatal编程技术网

Java 找不到类';android.support.v7.app.NotificationCompat$Builder';,从v4升级项目后

Java 找不到类';android.support.v7.app.NotificationCompat$Builder';,从v4升级项目后,java,android,notifications,builder,android-appcompat,Java,Android,Notifications,Builder,Android Appcompat,从v4升级项目后,找不到类“android.support.v7.app.NotificationCompat$Builder” 我正在尝试使用通知来管理我的mediaplyaer控件 我需要在我的项目中使用android.support.v7.app.NotificationCompat,以便根据此问题进一步研究锁屏控件 我已将我的支持库更新为23.0.1 当我尝试构建以下代码时 public class player6 extends Service implements OnComple

从v4升级项目后,找不到类“android.support.v7.app.NotificationCompat$Builder”

我正在尝试使用通知来管理我的mediaplyaer控件


我需要在我的项目中使用
android.support.v7.app.NotificationCompat
,以便根据此问题进一步研究锁屏控件

我已将我的支持库更新为
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);
    }
}
现在我知道我是否会使用
导入android.support.v4.app.NotificationCompat
it可以解决我当前的问题,但我需要使用
import-android.support.v7.app.NotificationCompat
来完成我未来的工作

问题发生在

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=

还试图添加cast(BULIDER),但每次都会出现相同的错误


我如何使用
android.support.v7.app.NotificationCompat
Builder
一切看起来都很好,试试看

  • 导入android.support.v7.app.NotificationCompat
  • 您不需要使用NotificationBuilder
这对我有帮助

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_ic_notification);

等等。我不知道为什么这必须如此格式化,但它对我有效。

你应该为此添加最新的支持库。
android.support.v7.app.NotificationCompat
已导入到我的项目中,但我不断收到升级我的项目的错误。我已经回答了这个问题。我已经尝试添加
android-support-v4.jar
android-support-v7-appcompat.jar
出现在
D:\android\android\u sdk\u details\extras\android\support\v7\appcompat\libs
文件夹中,但错误仍然存在。您已经给了我对这个问题的引用,我已经在下面了,请帮助我理解我在之前的评论中引用的导入和库是否正确;很好,但是请尝试更新您的支持库
Notification notificationBuilder = new NotificationCompat.Builder(
                this)
                .setSmallIcon(R.drawable.music)
                .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_stat_pause_100, "Stop",
                        makePendingIntent(BROADCAST_PLAYBACK_STOP))
                .addAction(R.drawable.ic_stat_pause_100, "Pause",
                        makePendingIntent(BROADCAST_PLAYBACK_PAUSE))
                .addAction(R.drawable.ic_stat_circled_right_2_100, "next",
                        makePendingIntent(BROADCAST_PLAYBACK_NEXT)).build();

        // Show notification
        startForeground(NOTIFICATION_ID, notificationBuilder);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_ic_notification);