I have trouble creating notification from the application in Android. API must be API 21 or higher. I've probably tried 100 ways with Android Developer and other websites.
minSDK must be API21
I opened it on API26
This is my activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click for Notification"
android:id="#+id/buckysButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="buckysButtonClicked" />
</RelativeLayout>
This is MainActivity.java
package pl.wat.pz.notification;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int uniqueID = 45612;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void buckysButtonClicked(View view){
//Build the notification
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setTicker("This is the ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Here is the title");
notification.setContentText("I am the body text of your notification");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
//Builds notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
}
And the manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.wat.pz.notification">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here image from android emulator
After click button nothing happened ;((
notification.setSmallIcon(R.mipmap.ic_launcher);
AFAIK, you cannot use a mipmap resource here, and your launcher icon is not a suitable notification icon on Android 4.4 and higher.
Create a separate drawable resource for your notification icon. If you are using Android Studio, the Image Asset wizard can assist with this.
Also, if your targetSdkVersion is 26 or higher, you need to set up a notification channel when running on Android 8.0+. This directory contains many sample apps that are up-to-date with respect to raising a Notification.
Related
I would like to put a Network Detector in an Android application, to inform a Screen immediately the Network state even if that state is changing. I have study similar questions in:
Android: Internet connectivity change listener
So my Java code in Main Activity is:
package com.example.netdetector;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private TextView Screen;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Screen = (TextView) findViewById(R.id.Screen);
Screen.setText("Start!");
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
#Override
public void onAvailable(Network network) {
// network available
Screen.setText("Net On!");
}
#Override
public void onLost(Network network) {
// network unavailable
Screen.setText("Net Off!");
}
};
ConnectivityManager connectivityManager =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
connectivityManager.registerDefaultNetworkCallback(networkCallback);
} else {
NetworkRequest request = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
connectivityManager.registerNetworkCallback(request, networkCallback);
}
}
}
The AndroidManifest.xml is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.netdetector">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.NetDetector">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The Layout activity_main.xml is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<TextView
android:id="#+id/Screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World!"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.222" />
</androidx.constraintlayout.widget.ConstraintLayout>
There are some issues:
Starting the App with the Network Off, the Screen displays "Start!" instead of "Net Off!"
Starting the App with the Network On, the Screen displays "Net On" that is fine
On changing Internet state while the app is running the Screen displays the right text for a few moments and the app closes immediately.
How can I fix 1 and 3? What went wrong?
Thanks
Nickolas
So I have an app that currently has two Activities. A SplashScreenActivity and a MainActivity. The transition between the two activities works just fine but the problem lies in the SplashScreenActivity itself. In it, there is a single ImageView that is supposed to be initially invisible and then fade in. But instead the image remains invisible the entire time. Please help. Here's the code:
The SplashScreenActivity:
package com.degioncloud;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
ImageView logo = (ImageView) findViewById(R.id.iv_logo);
logo.setImageAlpha(0);
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
});
}
}
The XML of the SplashScreenActivity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".SplashScreenActivity">
<ImageView
android:id="#+id/iv_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true" />
</RelativeLayout>
My Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.degioncloud">
<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/Theme.DegionCloud">
<activity android:name=".MainActivity" />
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The MainActivty is empty so I don't see a need to share that but if you need any other file then let me know and I will send a copy.
You need to call start() for the animation to begin:
logo.animate().alpha(1f).setDuration(1200).withEndAction(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
finish();
}
}).start();
Why do my android apps keep closing when using 2 intent? when I delete one of them, it's work. especially if I delete the "Reg" Button in the java files.
Thanks
XML Files:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".LoginAct">
<TextView
android:id="#+id/daftar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/daftar"
android:textAlignment="center"
android:textColor="#color/Linktext"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp">
</TextView>
<Button
android:id="#+id/pass"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:background="#color/colorPrimaryDark"
android:clickable="true"
android:focusable="true"
android:text="#string/lewat"
android:textColor="#color/textwhite" />
Java Files (If I deleted the "Reg" Button from this file, the app is working, but otherwise it will force close after the splash screen):
package com.soerja.ngalamhistory;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class LoginAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_login);
Button Pass = (Button) findViewById(R.id.pass);
Button Reg = (Button) findViewById(R.id.daftar);
Pass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, MainActivity.class);
startActivity(intent);
}
});
Reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(LoginAct.this, RegisAct.class);
startActivity(intent);
}
});
}
}
AndroidManifest Files:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.soerja.ngalamhistory">
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashAct"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginAct"
android:label="#string/applogin"
android:theme="#style/LogTheme"></activity>
<activity
android:name=".MainActivity"
android:label="#string/home"
android:theme="#style/AppTheme"></activity>
<activity android:name=".RegisAct"
android:label="#string/appdaftar"
android:theme="#style/LogTheme"></activity>
</application>
</manifest>
I would really appreciate your help because this is my school project :)
You are casting a TextView into a Button. Probably the problem is a invalid cast exception.
In XML, you define the "daftar" as a TextView, but in java you are trying to use it as a Button.
Change it
Button Reg = (Button) findViewById(R.id.daftar);
To it
TextView Reg = (TextView) findViewById(R.id.daftar);
Or change your XML implamentation, defining "daftar" as a Button
here you are using your text view id
android:id="#+id/daftar"
as a reference to declaring button
Button Reg = (Button) findViewById(R.id.daftar);
so i think ,that may be the reason for crash
try this method :
add this in your text xml code:
android:onClick="daftar"
add for java class use this:
public void daftar(View v)
{
Intent intent = new Intent(this, RegisAct.class);
startActivity(intent);
}
and remove the old java code you used for this text view:
i hope this helps.
I'm creating an Exoplayer video player which plays a mp4 file containing video and audio from a url. The video and audio plays properly but when the video player is paused, the audio is still being played. I don't know how I can fix this. I'm assuming that the problem occurs due to my android device. I'm leaving my code, app's info, android device info below and a video clip of my problem. If someone can use my code and tell me if they are experiencing the same problem, it will be thankful. Please Help Me.
The app's detail:
Using API 21: Android 5.0 (Lollipop). Contains Internet Permission. Importing implementation 'com.google.android.exoplayer:exoplayer:2.8.1'.
Device detail:
Type: Android Tablet. Android Version : 5.1.1
The MainActivity.java:
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MergingMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
SimpleExoPlayer video_player;
PlayerView player_screen;
DefaultTrackSelector track_selector;
DefaultBandwidthMeter band_width_meter = new DefaultBandwidthMeter();
MediaSource mediaSource_both;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
player_screen = findViewById (R.id.player_screen);
player_screen.requestFocus();
TrackSelection.Factory video_track_selection_factory = new AdaptiveTrackSelection.Factory(band_width_meter);
track_selector = new DefaultTrackSelector(video_track_selection_factory);
video_player = ExoPlayerFactory.newSimpleInstance(this, track_selector);
player_screen.setPlayer(video_player);
video_player.setPlayWhenReady(true);
DataSource.Factory data_source_factory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "Application Name"), new DefaultBandwidthMeter());
Uri url = Uri.parse("http://dash.akamaized.net/akamai/bbb/bbb_1280x720_60fps_6000k.mp4");
mediaSource_both = new ExtractorMediaSource.Factory(data_source_factory).createMediaSource(url);
video_player.prepare(mediaSource_both);
}
}
The activity_main.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"
tools:context=".MainActivity">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/player_screen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:use_controller="true" />
</android.support.constraint.ConstraintLayout>
Video Clip of the problem (The video has been uploaded on OpenLoad server):
https://openload.co/embed/NrlMUxUP0C4/problem.mp4
I made a very easy sample app to reproduce the issue
but yet too see any problem neither with the player or video
here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.player_view);
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector(new DefaultBandwidthMeter.Builder().build()));
SimpleCache downloadCache = new SimpleCache(new File(getCacheDir(), "exoCache"), new NoOpCacheEvictor());
String uri = "http://dash.akamaized.net/akamai/bbb/bbb_1280x720_60fps_6000k.mp4";
DataSource.Factory dataSourceFactory = new CacheDataSourceFactory(downloadCache, new DefaultDataSourceFactory(this, "seyed"));
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(uri));
player.addListener(new Player.EventListener() {
#Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
Log.d("exo", "timeLine Changed");
}
#Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(boolean isLoading) {
Log.d("exo", "loding changed= " + isLoading);
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
Log.d("exo", "state changed");
}
#Override
public void onRepeatModeChanged(int repeatMode) {
}
#Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
Log.e("exo", "exoplayer error", error);
}
#Override
public void onPositionDiscontinuity(int reason) {
}
#Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
#Override
public void onSeekProcessed() {
Log.d("exo", "seek processed");
}
});
player.prepare(mediaSource);
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(true);
}
here is the layout:
<android.support.design.widget.CoordinatorLayout 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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<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=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyDownloadService"
android:exported="false"/>
</application>
and lastly the build gradle file:
dependencies {
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.google.android.exoplayer:exoplayer:2.8.2'
}
Update 17/18/2018
Actually the problem you are facing is because of this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); line.
when you run your activity it starts in portrait mode and everything is fine (without this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);) and one exoplayer is created and starts the playback.
but when you change the orientation during startup of the activity. android trys to destroy the activity and create another one here comes your problem:
first activity does not get destroyed because you forget to realease the ExoPlayer in onDestroy() and when the new activity is created a new exoplayer is also created and both of them keep the playback going. when you pause the playback you actually pausing the second one not the first one. this behavior is called memory leak.
first of all make sure you call .release() in onDestroy() of the activity.
and then for orientation you have couple of options:
1.specify the orientation of the activity in the manifest so android apply it before the creation of the activity
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:screenOrientation="sensorLandscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2.prevent android from killing the activity on config changes:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
hope it helps you
you can also chek out this sample (with a small and minor difference) on github:
Hi have created splash screen as a separate project by using this link : http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/ . And it did worked on my emulator . However, when I implemented this in my app. It did not worked . I don' know whats wrong with my code. My main.java, splash screen.java and splash.xml are the following. Please note that I did define my activity in manifest file. Any help will be greatly appreciated...
MainActivity.Java
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
}
public void onClick(View v) {
int resId=1;
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
switch (v.getId()) {
case R.id.button_1: resId = R.raw.button_1; break;
case R.id.button_2: resId = R.raw.button_2; break;
case R.id.button_3:
startActivity(new Intent(MainActivity.this,SecondActivity.class));
return;
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
SplashScreen.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_background" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/images235" />
</RelativeLayout>
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Smit.yourbollywoodyourplaylist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="info.androidhive.androidsplashscreentimer.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name"/>
</application>
</manifest>
It's not clear which package your SplashScreen.java lives in, but
android:name="info.androidhive.androidsplashscreentimer.SplashScreen"
might need to be changed to
android:name=".SplashScreen"
You may have just pasted in the manifest definition from the sample project without adjusting the activity name to match your own project.