diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219CreateMusicList.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219CreateMusicList.java new file mode 100644 index 00000000..fb4a17d5 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219CreateMusicList.java @@ -0,0 +1,40 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903219; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Service; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.IBinder; +import android.widget.EditText; +import android.widget.Toast; + +import static android.support.v4.app.ActivityCompat.startActivityForResult; + +public class Net1314080903219CreateMusicList extends Service { + private static final int FILE_SELECT_CODE =5 ; + // private String listName; + Net1314080903219FileStoreReader f; + // final EditText inputServer = new EditText(this); + public Net1314080903219CreateMusicList() { + + + + } + + @Override + public IBinder onBind(Intent intent) { + // TODO: Return the communication channel to the service. + throw new UnsupportedOperationException("Not yet implemented"); + } + public int onStartCommand(Intent intent, int flags, int startId){ + + f=new Net1314080903219FileStoreReader(intent.getStringExtra("listName")); + String[] path=intent.getStringExtra("songPath").split("/"); + int l=path.length; + f.store(path[l-1]+":"+intent.getStringExtra("songPath")); + f.close(); + return START_STICKY; + } + +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219FileStoreReader.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219FileStoreReader.java new file mode 100644 index 00000000..7cb0f2b3 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219FileStoreReader.java @@ -0,0 +1,91 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903219; + +import android.os.Environment; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Created by j1 on 2016/5/6. + */ +public class Net1314080903219FileStoreReader { + private File object=null; + private FileInputStream fin=null; + private FileOutputStream fout=null; + private int fileLength=0; + public Net1314080903219FileStoreReader(String name){ + object=new File("/storage/sdcard1/Music/"+name+".txt"); + System.out.println(object.getPath()); + if(object.exists()){ + try { + fin=new FileInputStream(object); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return ; + } + } + + try { + fout=new FileOutputStream(object); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + public boolean close(){ + try { + if (fin!=null) { + fin.close(); + } + if(fout!=null){ + fin.close(); + } + + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } + + public int store(String data){ + if(fout==null){ + return -1; + } + byte[] by=data.getBytes(); + try { + fout.write(by); + fout.flush(); + } catch (IOException e) { + e.printStackTrace(); + return -1; + } + + return by.length; + } + + public int read(byte[] data){ + if(fin==null){ + return -1; + } + int length; + try { + length=fin.available(); + } catch (IOException e) { + e.printStackTrace(); + return -1; + } + if(length==0){ + return 0; + } + try { + fin.read(data); + } catch (IOException e) { + e.printStackTrace(); + } + return data.length>length?length:data.length; + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MainActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MainActivity.java index c25b953c..281ba66b 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MainActivity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MainActivity.java @@ -1,6 +1,12 @@ package edu.hzuapps.androidworks.homeworks.net1314080903219; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; + +import android.net.Uri; import android.os.Bundle; + import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; @@ -8,10 +14,17 @@ import android.view.View; import android.view.Menu; import android.view.MenuItem; -import android.widget.Button; -public class Net1314080903219MainActivity extends AppCompatActivity { +import android.widget.EditText; +import android.widget.Toast; + + +public class Net1314080903219MainActivity extends AppCompatActivity { + private String listName; + private final int LIST_CONTENT_CODE=100; + private final int FILE_SELECT_CODE=1000; + private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -19,14 +32,24 @@ protected void onCreate(Bundle savedInstanceState) { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { + FloatingActionButton email = (FloatingActionButton) findViewById(R.id.email); + email.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Snackbar.make(view, "contact us", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } + }); + + FloatingActionButton info = (FloatingActionButton) findViewById(R.id.info); + info.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) + Snackbar.make(view, "this is our displayer", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); + } @Override @@ -51,13 +74,77 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } public void onClick(View view){ - Button bu=(Button) findViewById(R.id.button); + /* Button bu=(Button) findViewById(R.id.button); if(bu.getText().equals("OPEN THE DOOR")){ bu.setText("SEE YOUR SISTER"); } else { bu.setText("OPEN THE DOOR"); - } + + }*/ + + final EditText inputServer = new EditText(this); + AlertDialog.Builder builder = new AlertDialog.Builder(this); + + builder.setTitle("List name").setIcon(android.R.drawable.ic_dialog_info).setView(inputServer) + .setNegativeButton("Cancel", null); + builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + listName = inputServer.getText().toString(); + + Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT); + intent1.setType("*/mp3"); + intent1.addCategory(Intent.CATEGORY_OPENABLE); + startActivityForResult(intent1, LIST_CONTENT_CODE); + + } + }); + builder.show(); + + + // startActivityForResult(intent,LIST_CONTENT_CODE); + // startService(intent); + } + public void play(View view){ + showFileChooser(); + + } + private void showFileChooser() { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("*/mp3"); + intent.addCategory(Intent.CATEGORY_OPENABLE); + try { + startActivityForResult(intent, FILE_SELECT_CODE ); + } catch (android.content.ActivityNotFoundException ex) { + Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show(); + } + } + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + switch (requestCode) { + case FILE_SELECT_CODE: + if (resultCode == RESULT_OK) { + // Get the Uri of the selected file + Uri uri = data.getData(); + intent = new Intent(); + intent.setClass(this, Net1314080903219MediaPlayerService.class); + intent.putExtra("song", uri.getPath()); + // System.out.println(intent.getStringExtra("song")); + startService(intent); + } + break; + case LIST_CONTENT_CODE: + if (resultCode == RESULT_OK) {{ + Uri uri = data.getData(); + intent = new Intent(); + intent.setClass(this, Net1314080903219CreateMusicList.class); + intent.putExtra("songPath",uri.getPath()); + intent.putExtra("listName",listName); + startService(intent); + }} + } + super.onActivityResult(requestCode, resultCode, data); + } } + diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MediaPlayerService.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MediaPlayerService.java new file mode 100644 index 00000000..31c6513c --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903219/Net1314080903219MediaPlayerService.java @@ -0,0 +1,84 @@ +package edu.hzuapps.androidworks.homeworks.net1314080903219; + +import android.app.Service; +import android.content.Intent; +import android.media.MediaPlayer; +import android.net.Uri; +import android.os.Binder; +import android.os.Environment; +import android.os.IBinder; +import android.widget.Toast; + +import java.io.IOException; + +import static android.support.v4.app.ActivityCompat.startActivityForResult; + +public class Net1314080903219MediaPlayerService extends Service { + MediaPlayer player = new MediaPlayer(); + + public Net1314080903219MediaPlayerService() { + + /* try { + player.reset(); + player.setDataSource(path); + player.prepare(); + } catch (IOException e) { + e.printStackTrace(); + }*/ + } + + + @Override + public IBinder onBind(Intent arg0) { + // TODO Auto-generated method stub + return null; + } + + //在这里我们需要实例化MediaPlayer对象 + public void onCreate(){ + + super.onCreate(); + //我们从raw文件夹中获取一个应用自带的mp3文件 + + System.out.println("sfsfsfsf dsf fdfd fsdf sf ffsfs"); + + + } + + /** + * 该方法在SDK2.0才开始有的,替代原来的onStart方法 + */ + public int onStartCommand(Intent intent, int flags, int startId){ + if(!player.isPlaying()){ + // System.out.println(String.valueOf(intent.getCharSequenceArrayExtra("song"))); + try { + player.reset(); + player.setDataSource(intent.getStringExtra("song")); + player.prepare(); + player.start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return START_STICKY; + } + + public void onDestroy(){ + //super.onDestroy(); + if(player.isPlaying()){ + player.stop(); + } + player.release(); + } + + + + + //后退播放进度 + public void haveFun(){ + if(player.isPlaying() && player.getCurrentPosition()>2500){ + player.seekTo(player.getCurrentPosition()-2500); + } + } +} + diff --git a/app/src/main/res/layout/activity_net1314080903219_main.xml b/app/src/main/res/layout/activity_net1314080903219_main.xml index bdcb4439..e20e8b83 100644 --- a/app/src/main/res/layout/activity_net1314080903219_main.xml +++ b/app/src/main/res/layout/activity_net1314080903219_main.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" - android:background="@drawable/Net1314080903219second" + android:background="@drawable/net1314080903219second" tools:context="edu.hzuapps.androidworks.homeworks.net1314080903219.Net1314080903219MainActivity"> + diff --git a/app/src/main/res/layout/content_net1314080903219_main.xml b/app/src/main/res/layout/content_net1314080903219_main.xml index 94e1b17e..ccfa31d3 100644 --- a/app/src/main/res/layout/content_net1314080903219_main.xml +++ b/app/src/main/res/layout/content_net1314080903219_main.xml @@ -16,12 +16,20 @@