Am trying to create a webview app displaying a splash screen once after the first lunch. Initially once i open the app the splash screen will show up then after 5 seconds it will load main activity VaultActivity, but after i have added the line of code to check if splash screen 'SplashScreen' has been launched before, the app stopped loading VaultActivity using the SPLASH_TIME_OUT i set and also the splash screen still shows up anytime i lunch the app.
Initially
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 5000; // Splash screen timer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start main activity
Intent intent = new Intent(SplashScreen.this, VaultActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Currently
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 5000; // Splash screen timer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false)){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start main activity
Intent intent = new Intent(SplashScreen.this, VaultActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
} else {
SharedPreferences.Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
}
}
My Manifest
<activity
android:name=".SplashScreen"
android:launchMode="singleTask"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".VaultActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="example.com"
android:pathPrefix="/androidmobile" />
</intent-filter>
</activity>
:) you need just adding startActivity in else section.
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 5000; // Splash screen timer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
final SharedPreferences.Editor ed = pref.edit()
if(pref.getBoolean("activity_executed", false)){
//ed.putBoolean("activity_executed", true);
//ed.commit();
Intent intent = new Intent(SplashScreen.this, VaultActivity.class);
startActivity(intent);
finish();
} else {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Start main activity
Intent intent = new Intent(SplashScreen.this, VaultActivity.class);
startActivity(intent);
ed.putBoolean("activity_executed", true);
ed.commit();
finish();
}
}, SPLASH_TIME_OUT);
}
}
}
You have to call the startActivity in your else.
Related
My app is starting twice and the weird thing is, it depends on the LOAD_TIME which activity is called twice. With a long delay, like >5000ms, my Main Activity is called twice. As you can see in the snap shot, with 2000ms, my startup activity and my run() Thread is called twice.
It also only happens on my emulator, if I use my physical phone everything runs fine. I thought it might has something to do with the Consumer class, which requires a newer build version. But I think it requires 24 and I am using Nexus 5 with version 30 so I should be fine.
Ami doing something wrong with the lifecycle or threading?
public class StartUpActivity extends AppCompatActivity {
public final int LOAD_TIME = 2000;
public static int counter;
final Handler handler = new Handler(Looper.getMainLooper());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
counter++;
Log.d("debug counter startup: ", counter+"");
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_start_up);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titel_bar);
handler.postDelayed(new Runnable() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void run() {
Log.d("debug Handler: ", "run()");
PreLogin checkLoginStatus = new PreLogin();
checkLoginStatus.CheckIfStillLoggedIn((value) -> {
if(value==true){
Log.d("debug checkLogin", "checkIfStillLoggedIn=true");
Intent intent = new Intent(StartUpActivity.this, MainActivity.class);
intent.putExtra("caller", "StartUpActivity");
startActivity(intent);
finish();
}
else{
Log.d("debug checkLogin", "checkIfStillLoggedIn=false");
Intent intent = new Intent(StartUpActivity.this, ChooseLoginOrRegistrationActivity.class);
intent.putExtra("caller", "StartUpActivity");
startActivity(intent);
finish();
}
});
handler.removeCallbacks(null);
}
}, LOAD_TIME);
}
}
AndroidManifest:
<activity
android:name=".StartUpActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I want to open login_activity on first time entering app, and then on the second entering to app open main_activity.
I create something but it wont work. so I wonder what I'm doing wrong?
this is my LoginActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = (EditText) findViewById(R.id.username);
userPhone = (EditText) findViewById(R.id.userPhone);
loginBtn = (Button) findViewById(R.id.buttonLogin);
dbHandler = new LogsDBHandler(this);
loginBtn.setOnClickListener(this);
setTitle("AMS - biomasa | prijava");
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if (pref.getBoolean("activity_executed", false)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
SharedPreferences.Editor edt = pref.edit();
edt.putBoolean("activity_executed", true);
edt.commit();
}
}
public void insert() {
User user = new User (
userName.getText().toString(),
userPhone.getText().toString());
dbHandler.addUser(user);
Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v) {
if (v == loginBtn && validateUser()) {
insert();
}
}
In main activity i have only image and two buttons.
And in manifest I add launcher to main and login activity.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
What am I doing wrong here?
Create one start-up activity call it as SplashActivity
public class SplashActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// decide here whether to navigate to Login or Main Activity
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if (pref.getBoolean("activity_executed", false)) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
}
In your LoginActivity simply set activity_executed to true
public void insert() {
User user = new User (
userName.getText().toString(),
userPhone.getText().toString());
dbHandler.addUser(user);
Toast.makeText(getBaseContext(), "Prijavljeni ste!", Toast.LENGTH_SHORT).show();
//set activity_executed inside insert() method.
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
SharedPreferences.Editor edt = pref.edit();
edt.putBoolean("activity_executed", true);
edt.commit();
}
change manifest as below-
<activity android:name=".MainActivity"/>
<activity android:name=".LoginActivity" />
<activity android:name=".SplashActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
you can change launcher activity as main activity.so that when you open the application it is starting from main activity there you can check whether he is logged in or not.if he is not logged in you must navigate him to login activity or else you just do it as it is.Following is manifest file..
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity"></activity>
You should add another empty activity (with no UI) that loads before anything.
Then use SharedPreferences to store some value. Thus if the user has already opened your app once, the value is stored. And then use a condition to check this value. If its the value you saved skip login_activity and direct to main_activity else direct to login_activity.
Problem about the line
if (pref.getBoolean("activity_executed", false)) {
You can Implement this method to call inside if(appIsLoggedIn)
public boolean appIsLoggedIn(){
return pref.getBoolean("activity_executed", false);
}
I created one app with two activities when I run app It opens first launcher activity In this activity I added textViewon clicking this textViewsecond activity is opened,
At the second activity again I added one textView on clicking that textView I expected to launch my first activity(Launcer activity) but this doesn't happens? Why? My Manifest file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adbs.abs.dhanagarmaza" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".LoginRegi"
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>
<activity
android:name=".Register"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="com.adbs.abs.REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent("android.intent.action.MAIN");
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent("com.adbs.abs.REGISTER");
startActivity(openRegister);
}
});
}
Activity 1:-
public class MainActivity extends Activity {
TextView tvOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvOne=(TextView)findViewById(R.id.tvOne);
tvOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(intent);
}
});
}
}
Activity 2:-
public class Main2Activity extends Activity {
TextView tvTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tvTwo=(TextView)findViewById(R.id.tvTwo);
tvTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
});
}
}
Intent i =new Intent(this,MainActivity.class);
startActivity(i);
you can post your code so we can get exact problem because ..
by finish(); you can get your prevous activity back either you can do it by Intent
My LoginRegi(Second activity java file 'DEFAULT activity')
protected void onCreate(Bundle registerBundle) {
super.onCreate(registerBundle);
setContentView(R.layout.register);
// If user wants to login then on click "Login Me" textView open activity(LoginRegi.xml)
loginMe = (TextView)findViewById(R.id.tvLoginMe);
loginMe.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openLoginRegi = new Intent(Register.this,LoginRegi.class);
startActivity(openLoginRegi);
}
});
}
and LoginRegi.java (First activity 'LAUNCHER activity')
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_regi);
// On click signup textView => Open activity (register.xml)
signUp = (TextView)findViewById(R.id.tvSignUp);
signUp.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent openRegister = new Intent(LoginRegi.this,Register.class);
startActivity(openRegister);
}
});
}
I am creating an android app and when I go to debug it on my samsung galaxy the Splash activity loads first,as it should, but after that the app crashes/stops right after doing the "Splash" activity. It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. Does anyone know what might be causing the problem? Plus after I tried debugging the app and loaded it onto my phone the app isn't even showing up. I am using Eclipse by the way. It shows the app in my application manager on my phone but it doesn't show the icon in my app screen.
Here is my Splash.java:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainActivity = new Intent("com.example.mihirandroidsapp.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Here is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.mihirsandroidapp.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.mihirsandroidapp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And here is my main activity which should start after the splash screen:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter += 1;
display.setText("Total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter -= 1;
display.setText("Total is " + counter);
}
});
}
}
Oh.. Where do I start.. Let's go through all of the issues:
1) Fix your manifest. Definitely not the right way to declare your activities. Here is what it should look like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
2) Now let's fix the way you start your activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
3) Don't call finish() in onPause() - you break native activity lifecycle flow. Call finish() right after you start new activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
4) Instead of creating separate thread, just a create a Handler and post Runnable there with 5 seconds delay:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//this will be called after 5 seconds delay
}
}, 5000);
Here is entire file put together:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000);
}
If it doesn't help - we definitely need to look at logcat output...
A simple way to achieve this if one is ready to compromise on drawing performance is to define custom theme with splash image that one wants to use as window background and use this custom theme as application theme
styles.xml
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/background_image</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml
<application
android:debuggable="true"
android:icon="#drawable/icon"
android:theme="#style/CustomTheme"
android:label="#string/app_name">
...
</application>
This would use the #drawable/background_image as the window.background. As a result if the activities has transparent background then #drawable/background_image will be visible as activities background. One can avoid this by setting appropriate color or drawable in onCreate of every activity programatically as
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutResID);
activity.getWindow().setBackgroundDrawableResource(R.color.window_bg);
}
Check this for more information
All what you need for a splash screen
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_DURATION = 1000;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_DURATION);
}}
In drawables create this bg_splash.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/app_color"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_in_app_logo_big"/>
</item></layer-list>
In styles.xml create a custom theme
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
</style>
and finally in AndroidManifest.xml specify the theme to your activity
<activity
android:name=".activities.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Cheers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread th = new Thread(new Runnable() { /*create a new thread */
#Override
public void run() { /*
* The purpose of this thread is to
* navigate from one class to another
* after some time
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
/*
* We are creating this new thread because we don’t
* want our main thread to stop working for that
* time as our android stop working and some time
* application will crashes
*/
e.printStackTrace();
}
finally {
Intent i = new Intent(MainActivity.this,
Splash_Class.class);
startActivity(i);
finish();
}
}
});
th.start(); // start the thread
}
http://www.codehubb.com/android_splash_screen
I have added splash screen by using following code:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
initConstatnts();// method for intilizing any constants
new Thread(new Runnable() {
#Override
public void run() {
if (!isFinishing()) // checking activity is finishing or not
{
try {
Thread.sleep(3000);//delay
Intent i = new Intent(getBaseContext(),
HomeActivity.class);
startActivity(i);
finish();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
}
private void initConstatnts() {
}
}
Flow the below steps to Create your own splash screen
Create Activity1 and XML file
Design the XML file and put the welcome message
Crete Activity2 and XML file.
Start the Activity1
Start a Thread in Activity1 and sleep for 5 seconds
Start Activity2 from the Thread
There is nothing called readymade splash screen in Android . we can achieve that using above steps.
In a single line, start Activity1 wait for 5 sec then start Activity2.
So user will feel that first screen is splash screen.
You can download the complete code from below link
http://javaant.com/splash-screen-android/#.VwzHz5N96Hs
i have faced the same problem.... i think you code is perfect for the app
u just try with this in the Intent Creation in your splash activity class
Intent openMainActivity = new Intent("android.intent.action.MAIN");//MAIN is the that u want to start
//next after the current Activity
Basically I'm just trying to run a class which is called NotificationReceiverActivity from another Activity class, but nothing shows up on the screen when I click on the button to run the activity class, I feel like I'm being a total noob on the xml part of the code and here is the AndroidManifest.xml ;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.notificationmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="de.vogella.android.notificationmanager.CreateNotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="de.vogella.android.notificationmanager.NotificationReceiverActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The main activity which is CreateNotificationActivity class, works and turns on flawlessly.
Java code for the main activity;
public class CreateNotificationActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(0, noti);
}
}
And the NotificationReceiverActivity class;
public class NotificationReceiverActivity extends Activity implements OnClickListener {
private final String CLASSNAME = getClass().getSimpleName();
Camera cam = null;
ImageButton ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wl.acquire();
initialize();
ib1.setOnClickListener(this);
Log.i(CLASSNAME, "CREATING NOW"+cam);
}
private void initialize() {
// TODO Auto-generated method stub
ib1 = (ImageButton) findViewById(R.id.ib2);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (cam == null) {
cam = Camera.open();
para = cam.getParameters();
para.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(para);
Log.i(CLASSNAME, "AA"+cam);
} else {
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
Log.i(CLASSNAME, "BB"+cam);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
cam=cam;
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
cam=cam;
}
}
And the top 4 lines of the log cat;
01-31 06:11:10.582: E/AndroidRuntime(29533): FATAL EXCEPTION: main
01-31 06:11:10.582: E/AndroidRuntime(29533): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.vogella.android.notificationmanager/de.vogella.android.notificationmanager.NotificationReceiverActivity}: java.lang.SecurityException: Neither user 10181 nor current process has android.permission.WAKE_LOCK.
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-31 06:11:10.582: E/AndroidRuntime(29533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
Remove the intent filter from your NotificationReceiverActivity.
to use a button to start NotificationReceiverActivity from you CreateNotificationActivity you have to assign a clicklistener to that button in you activity
This is an example of this
public class CreateNotificationActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(CreateNotificationActivity.this,NotificationReceiverActivity.class);
startActivity(intent);
}
});
}
}