我是Android Studio的新手。我目前正在构建一个应用程序,允许您在锁定或解锁手机屏幕时播放音乐。它与媒体播放器类似,但在您锁定和解锁手机时播放音乐。
此应用的想法是阅读手机的sdCard并获取音乐文件。之后,您可以选择要设置为锁定/解锁声音的音乐。
我已经将音乐列表中的文件传回mainActivity(不确定我是否正确行事)。
我现在的问题:当用户点击确认按钮时,设置锁定和解锁屏幕以播放所选音乐。
这是我的代码
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ConfirmMessage(View view){
Toast.makeText(this, "Sound has been set", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void GetMusicListLock(View view) {
Intent getNameScreenIntent = new Intent(this, SecondScreen.class);
final int resultLock=1;
startActivityForResult(getNameScreenIntent, resultLock);
}
public void GetMusicListUnlock(View view) {
Intent getNameScreenIntent = new Intent(this, SecondScreen.class);
final int resultUnlock=2;
startActivityForResult(getNameScreenIntent, resultUnlock);
}
@Override
//get result from the chosen music
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TextView lockMusic = (TextView) findViewById(R.id.LocktextView);
TextView unlockMusic = (TextView) findViewById(R.id.UnlocktextView);
if (requestCode == 1 && resultCode == RESULT_OK) {
String chosenLockMusic = data.getStringExtra("dataSend");
lockMusic.setText(chosenLockMusic);
String lName = data.getStringExtra("lname");
File file = new File(lName);
Toast.makeText(this, file.getName().toString(), Toast.LENGTH_LONG).show();
}
if(requestCode==2 && resultCode == RESULT_OK){
String chosenUnlockMusic = data.getStringExtra("dataSend");
unlockMusic.setText(chosenUnlockMusic);
String lName = data.getStringExtra("lname");
File file = new File(lName);
Toast.makeText(this, file.getName().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
SecondScreen(音乐列表视图)
public class SecondScreen extends Activity {
ListView lv;
String [] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
Intent activityThatCalled = getIntent();
lv = (ListView) findViewById(R.id.lvMusicList);
//Array for finding Music File
ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
//For storing the music names
items = new String[mySongs.size()];
//Show music names
for(int i =0; i<mySongs.size(); i++){
//toast(mySongs.get(i).getName().toString());
items[i]= (mySongs.get(i).getName().toString().replace(".mp3","").replace(".wav",""));
}
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), R.layout.song_layout, R.id.textView, items);
lv.setAdapter(adp);
//Listener for when the music is chosen
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data_send = (String)lv.getItemAtPosition(position);
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath);
File f2= new File(file.getAbsolutePath());
String path = f2.getAbsolutePath();
Intent goingBack = new Intent();
goingBack.putExtra("dataSend", data_send).putExtra("lname", path);
setResult(RESULT_OK, goingBack);
finish();
}
});
}
//function for findSongs
public ArrayList<File>findSongs(File root){
ArrayList al = new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files){
if(singleFile.isDirectory() && !singleFile.isHidden()){
al.addAll(findSongs(singleFile));
}
else{
if(singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")){
al.add(singleFile);
}
}
}
return al;
}
// public void toast(String text){
// Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
//}
}
LockScreenService
public class LockScreenService extends Service {
BroadcastReceiver receiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
@SuppressWarnings("deprecation")
public void onCreate() {
//Start listening for the Screen On, Screen Off, and Boot completed actions
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//Set up a receiver to listen for the Intents in this Service
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
super.onCreate();
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
LockScreenReceiver。我想这是我设置锁定和解锁屏幕声音的地方?但我不知道怎么做。我读到您可以使用共享偏好设置,但我不知道如何在我的代码中实现它。
public class LockScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.equals("android.intent.action.SCREEN_OFF"))
{
}
else if (intent.equals("android.intent.action.SCREEN_ON"))
{
}
}
}
有关更深入的信息,请参阅我的源代码链接。 https://github.com/PiersonLeo94/Lock_Screen_Sound
与我想要构建的功能类似的应用程序。 https://play.google.com/store/apps/details?id=com.screensound.ron.screensound&hl=en
感谢。