我制作的应用程序可以启动其他应用程序,例如netflix。我在第一个XML文件上有图像按钮,它将成为"收藏夹"。当您单击此图像按钮时,您将启动要启动的应用程序的意图。然后应用程序打开。如何在用户使用之后自动更改收藏夹。
这是我的Java代码:
package com.carlo_projekt.tvprograms;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Locale;
' public class MainActivity extends AppCompatActivity {
ImageButton speakBtn;
Button CategoriesBtn;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speakBtn = (ImageButton) findViewById(R.id.SpeakImageBtn);
CategoriesBtn = (Button) findViewById(R.id.CategoriesBtn);
text = (TextView) findViewById(R.id.textView);
}
public void OpenGame(View view) {
Intent intent = new Intent(this, MoreActivity.class);
startActivity(intent);
}
public void Speak(View view)
{
Intent recognizeIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizeIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizeIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
recognizeIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hi! Which app do you want to start?");
try {
startActivityForResult(recognizeIntent, 1);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(MainActivity.this, "Sorry! Your device doesn't support this function!", Toast.LENGTH_LONG).show();
}
}
public void onActivityResult(int request_Code, int result_Code, Intent recognizeIntent)
{
super.onActivityResult(request_Code, result_Code, recognizeIntent);
switch (request_Code)
{
case 1: if(result_Code == RESULT_OK && recognizeIntent != null)
{
ArrayList<String> result = recognizeIntent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
for(int i = 0; i<result.size(); i++)
{
result.set(i, result.get(i).toLowerCase());
if(result.get(i).compareTo("start netflix") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("com.netflix.mediaclient");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start youtube") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start viafree") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("se.viafree.android");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start tvfour") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("se.tv4.tv4playtab");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start dplay") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("se.kanal5play");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start viaplay") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("com.viaplay.android");
startActivity(NetflixIntent);
}
else if(result.get(i).compareTo("start svtplay") == 0)
{
Intent NetflixIntent = getPackageManager().getLaunchIntentForPackage("se.svt.android.svtplay");
startActivity(NetflixIntent);
}
/**text.setText(result.get(i));*/
}
}
}
}
public void OpenApps(String packageName)
{
if(getPackageManager().getLaunchIntentForPackage(packageName) != null)
{
Intent tv4PlayIntent = getPackageManager().getLaunchIntentForPackage(packageName);
startActivity(tv4PlayIntent);
}
else
{
String marketAppPackageName = "market://details?id="+ packageName;
MessageBox(marketAppPackageName);
}
}
public void NetflixFunction(View view)
{
String netflixPackageName = new String("com.netflix.mediaclient");
OpenApps(netflixPackageName);
}
public void ViafreeFunction(View view)
{
String viafreePackageName = new String("se.viafree.android");
OpenApps(viafreePackageName);
}
public void YoutubeFunction(View view)
{
String youtubePackageName = new String("com.google.android.youtube");
OpenApps(youtubePackageName);
}
public void Tv4PlayFunction(View view)
{
String tv4playPackageName = new String("se.tv4.tv4playtab");
OpenApps(tv4playPackageName);
}
public void LayoutsShow(View view)
{
RelativeLayout layoutShow = findViewById(R.id.LayoutShow);
if (layoutShow.getVisibility() == View.GONE) {
layoutShow.setVisibility(View.VISIBLE);
}
else if (layoutShow.getVisibility() == View.VISIBLE) {
layoutShow.setVisibility(View.GONE);
}
}
public void MessageBox(final String uriIntent)
{
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Couldn't find this app");
String messageText = "We cant find this app, do you want to download it?";
alertDialog.setMessage(messageText);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent MarketIntent = new Intent(Intent.ACTION_VIEW);
MarketIntent.setData(Uri.parse(uriIntent));
startActivity(MarketIntent);
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}}
这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
tools:context="com.carlo_projekt.tvprograms.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<Button
android:id="@+id/CategoriesBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:background="@drawable/round_corners"
android:onClick="OpenGame"
android:text="Categories"
android:textAllCaps="false" />
<ImageButton
android:id="@+id/NetflixImageBtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/CategoriesBtn"
android:onClick="NetflixFunction"
app:srcCompat="@drawable/netflix" />
<ImageButton
android:id="@+id/YoutubeImageBtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignTop="@+id/NetflixImageBtn"
android:layout_toEndOf="@+id/CategoriesBtn"
android:onClick="YoutubeFunction"
app:srcCompat="@drawable/youtube" />
<ImageButton
android:id="@+id/ViafreeImageBtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignStart="@+id/NetflixImageBtn"
android:layout_below="@+id/NetflixImageBtn"
android:layout_marginTop="52dp"
android:onClick="ViafreeFunction"
app:srcCompat="@drawable/viafree" />
<ImageButton
android:id="@+id/Tv4PlayImageBtn"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignEnd="@+id/YoutubeImageBtn"
android:layout_alignTop="@+id/ViafreeImageBtn"
android:onClick="Tv4PlayFunction"
app:srcCompat="@drawable/tv4" />
<ImageButton
android:id="@+id/SpeakImageBtn"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:onClick="Speak"
app:srcCompat="@drawable/microphone" />
<ImageButton
android:id="@+id/imageButton19"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
app:srcCompat="@drawable/sweden" />
<ImageButton
android:id="@+id/imageButton24"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/imageButton19"
app:srcCompat="@drawable/great_britain" />
<ImageButton
android:id="@+id/imageButton22"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/imageButton24"
app:srcCompat="@drawable/germany" />
<ImageButton
android:id="@+id/imageButton23"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/imageButton22"
app:srcCompat="@drawable/france" />
<ImageButton
android:id="@+id/imageButton25"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/imageButton23"
app:srcCompat="@drawable/spanish" />
<TextView
android:id="@+id/textView"
android:layout_width="193dp"
android:layout_height="33dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="217dp"
android:layout_marginEnd="90dp"
android:text="TextView"
android:textColor="#ff00" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
`
我对堆栈溢出很新,所以我希望你能理解我想要做什么。
答案 0 :(得分:0)
获取SharedPreferences对象:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
创建一个点击侦听器,该监听器将更新相应按钮的点击次数:
View.OnClickListener myClickListener= new View.OnClickListener() {
public void onClick(View v) {
String tag = (String) v.getTag();
sharedPref
.edit()
.putInt(tag, shardPref.getInteger(tag) + 1)
.apply();
}
};
现在,对于每个按钮,您都将同时设置点击侦听器和标记以进行命名:
Button btn = findViewById(R.id.NetflixImageBtn);
btn.setTag("netflix");
btn.setOnClickListener(myClickListener);
现在,当您单击按钮时,它将增加与标签关联的计数。您可以根据需要使用该信息。