gpt4 book ai didi

java - 启动画面和 Intent

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:52 25 4
gpt4 key购买 nike

我正在开发这个 Android 应用程序,它从图库接收数据(在我的例子中是图像)并显示它。我还必须做的是为我的应用程序创建一个 SplashScreen 。但当我这样做时,我的 Intent 就变得无效了。

Manifest code:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xkbc1923.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>
<activity android:name=".AlertExampleActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
</application>

SplashScreen

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {
private static final int DISPLAY_DURATION = 1000;
@Override
protected final void onCreate(final Bundle savedInstState) {
super.onCreate(savedInstState);
setContentView(R.layout.activity_splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, AlertExampleActivity.class);
startActivity(i);
// close this activity
finish();
}
}, DISPLAY_DURATION);
}
}

MainActivity

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import static android.provider.CalendarContract.CalendarCache.URI;
public class AlertExampleActivity extends AppCompatActivity {
ImageView picView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
///get the image view
//get the text view
setContentView(R.layout.activity_alert_example);

picView = (ImageView) findViewById(R.id.picture);
TextView txtView = (TextView) findViewById(R.id.txt);

if (txtView ==null) {
Log.w("Example", "TextView is null");
}

//get the received intent
Intent receivedIntent = getIntent();
//get the action
String receivedAction = receivedIntent.getAction();
//find out what we are dealing with
String receivedType = receivedIntent.getType();
//make sure it's an action and type we can handle
if (receivedAction.equals(Intent.ACTION_SEND)) {
Log.d("Example", "Send received");
//content is being shared
if (receivedType.startsWith("text/")) {
Log.d("Example", "Text received");

//handle sent text
//hide the other ui item
picView.setVisibility(View.GONE);
//get the received text
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
//check we have a string
if (receivedText != null) {
//set the text
txtView.setText(receivedText);
}
} else if (receivedType.startsWith("image/")) {
Log.d("Example", "Image received");
//handle sent image
handleSendImage(receivedIntent);
}

} else if (receivedAction.equals(Intent.ACTION_MAIN)) {
//app has been launched directly, not from share list
Log.d("Example", "Direct launch of App");
}
}

private void handleSendImage(Intent intent) {
// Get the image URI from intent
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
// When image URI is not null
if (imageUri != null) {
// Update UI to reflect image being shared
picView.setImageURI(imageUri);
} else{
Toast.makeText(this, "Error occured, URI is invalid", Toast.LENGTH_LONG).show();
}
}
}

最佳答案

您没有向您的 Intent 添加任何额外的数据。所以正常情况下它会是空的。只需将必要的数据添加到 SplashScreen.java 上的 Intent (i) 即可。前-

i.setAction("action"); 
i.setType("type");
i.putExtra("key", "value");

关于java - 启动画面和 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43497512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com