Use of thread is not working properly - java

I am making an app using aide app,when using thread in it & install it then the smart manager of Samsung device shows that the app contains malware.
My codes are for MainActivity.java
package com.mycompany.staticgk;
import android.app.*;
import android.content.*;
import android.os.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent i = new Intent(this,SplashActivity.class);
Thread timer= new Thread (){
public void run(){
try {
try
{
sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
finally {
startActivity(i);
finish();
}
}
};
timer.start();
}
}
while for SplashActivity.java
package com.mycompany.staticgk;
import android.app.*;
import android.os.*;
public class SplashActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
Please,correct me if anywhere i am wrong.

Is not error on your code.its security based problem .dont install apk with unauthorized app they may be hack your phone informations
Best Use Android Studio [The official Android IDE] to develope code and generate apk with signature key then only
mobile allows to install apk on device.All the native app are developed through android studio only.
otherwise enable Unknown source in your device it may be install on device.
Settings>>Lock screen and security>> tap on toggle button of Unknown Sources.

There are a lot of ways to go to MainActivity from SplashActivity one way could be :
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
startActivity(new Intent(MainActivity.class,SplashActivity.class););
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Also I see something weird with that double try catch block put just one.
Happy coding

Related

Update TextView with WiFi Signal Strength. Android Studio. Writing in Java

I am getting several errors and can't discover which is causing the problem.
Errors:
Cannot resolve symbol
Unexpected Token (while loop)
Unknown class "x"
How can I set the textView to display the wifiSignal strength?
Sorry for not narrowing down the problem more, but I am new to developing in android studio and am too lost to be specific.
Thanks so much for any help you can provide!
Code:
package com.example.a18lts01.wificompass;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} }
class wifi
{
#Override
public void run() {
}
int x = 1;
while(x == 1)
{
int signalLevel = result.level;
TextView.setText(signalLevel + "db");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread myThread = new Thread(myRunnable);
myThread.start();
}
}
}
Looks like you've got some misplaced brackets. I'm assuming you want your int x = 1; etc. to be inside the run() method? Currently you have an empty run() method.
Double-check the placement of the brackets. It looks like you need to remove the } a couple of lines after public void run() {, and add a } at the end of the wifi class.

Advice on how to fix frame skipping in android

So I've just noticed that my app is skipping quite a few frames when running in the emulator. This is my first app and I did some reading on the topic and found that I might not be starting the activities correctly. However, my activities are loaded through the settings menu and I don't know where this is in my code. If this is a big issue it would be appreciated if someone could point me in the right direction in relation to my specific code?
https://github.com/addrum/Calculate
I can post code here in preference if needed.
Edit: It appears to skip frames on the splash activity:
package com.main.androidcalculator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try{
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMain = new Intent("com.main.androidcalculator.MAINACTIVITY");
startActivity(openMain);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
I think the emulator is just too slow.
Your code works fine on a real device. I've tested on GS3.
Maybe ProgressBar is just too heavy for the emulator.
The view has animation and a lot of stuffs.
(Remove the ProgressBar and the issue's gone!)
See also:
Choreographer(639): Skipped 50 frames

What is causing this button to crash?

I'm writing an app that allows me to send a command through TCP/IP by clicking a button. However when I click to the button in the emulator it comes back with a message saying the button has stopped working. I was wondering if anyone could spot the error in my code.
CODE:
package button.test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class ButtonActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) throws UnknownHostException, IOException
{
Socket socket = new Socket("192.168.2.92", 60128);
try{
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="ISCP000000100000000701000000!1PWR010D";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch(IOException e)
{
//error code
}
}
private void write(Object BUFFER) {
// TODO Auto-generated method stub
}
}
1. You missed declaring the button, and initializing it...
Eg:
public class ButtonActivity extends Activity {
Button mbutt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mbutt = (Button)findViewById(R.id.Button_Click);
}
- Make a note that you must initialize the views only after the setContentView(), else your views won't get the id and will make your App to crash.
2. Its always advisable to keep the UI work on UI thread, and Non-UI work on Non-UI thread, but that became a law with the arrival of HoneyComb android version.
3. You can use Thread with a Handler to sync UI and Non-UI thread.
4. AsyncTask which is known as Painless Threading was introduced specially in android for this.
See this link for tutorials on Threads, Handlers and AsyncTask:
http://www.vogella.com/articles/AndroidPerformance/article.html
It looks like the socket connection work is taking a long time. Suggest putting this functionality inside an AsyncTask.
You haven't declared your button in onCreate()
You don't need to declare the button if you are inflating an xml.
Your problem is that you run a Network connectivity on a main Thread which is not allowed since api 11. Use a asynctask or a thread for this.
e.g.
new Thread() {
public void run() {
Socket socket = new Socket("192.168.2.92", 60128);
try{
dataOutputStream.Object BUFFER = null;
write(BUFFER);
String buffer="ISCP000000100000000701000000!1PWR010D";
OutputStream os=socket.getOutputStream();
os.write(buffer.getBytes());
} catch(IOException e)
{
//error code
}
}
}.start();

android Thread and Service

I am using below code.
import android.app.Activity;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.util.Log;
public class ActivityDemoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable() {
#Override
public void run() {
int i=0;
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("DATA", "Data.CONTENT_TYPE......"+(i++));
}
}
}).start();
}
#Override
public void onBackPressed(){
finish();
}
}
When I press Back button of device still thread is working in background.
My questions are...
when thread will stop?
Then why we required services in android?
Thank You.
Answers:
1 - it will stop after 100 milliseconds
2 - you need something to work in the background like a service that wakes up and
performs a function without there being a user to initiate it. Or you want to have
a progressDialog that tells the user some work is happening rather than let the user
assume the UI is frozen. Without the background service, your UI is left with an app
that doesn't respond while the work is happening.
Services are basically used for performing long-running application on the background.If you are using any network operations or playing musing or something like that, services are very much helpful..

Create opening application

I would like to display an image at the opening of my application Android (Java), is like a toast in full screen only.
That is, when you open the application and an image appears and disappears after you start the program.
What better way to do this?
You mean Splash screen? If so, here is your answer: Splash Screen
It's called a SplashScreen
the links are here
http://www.anddev.org/simple_splash_screen-t811.html
and here
http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
Cheers
I think you are looking for Splash screen in android.
I found above link really helpful. There are lots of other article available. Just ask Google
You have to create an Activity with this image on the layout.
Then within this activity, create a Thread that will sleep for X seconds. When the Thread slept enough, start a new activity.
This is an example code :
public class SplashActivity extends Activity {
public StartThread th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
th = new StartThread(this);
th.start();
}
}
class StartThread extends Thread {
SplashActivity parentActivity;
public StartThread(SplashActivity splashActivity) {
this.parentActivity = splashActivity;
}
#Override
public void run() {
super.run();
try {
this.sleep(3000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
Intent menu = new Intent("com.yourpackage.yourapplication.NEXTACTIVITY");
this.parentActivity.startActivity(menu);
this.parentActivity.finish();
this.parentActivity.th = null;
}
}
}

Categories

Resources