how to start new activity in android - java

I want to start a new activity called Counter when a button is clicked, but I got an error that is the activity is not found...so where is the wrong in my code:
t = new Thread(){
public void run(){
try{
sleep(5000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
}
}
};
test.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
t.run();
}
});
this is the manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.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="com.example.test.Counter"
android:label="#string/title_activity_counter" >
</activity>
</application>
</manifest>

Intent intent = new Intent(MyActivity.this, OtherActivity.class);
startActivity(intent);

Change this
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
This is called implicit intent and it needs intent filter
to
Intent counter = new Intent(MainActivity.this,Counter.class);
startActivity(counter);
This is called explicit intent and there is no need for intent filter
You should use explicit intent coz you have
<activity
android:name="com.example.test.Counter"
android:label="#string/title_activity_counter" >
</activity>
Quoting docs
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.
Edit:
You should call start() on the thread not run

You can replace this
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
with this, it would work ..
Intent counter = new Intent(MainActivity.this, Counter.class);
startActivity(counter);
Or to let your code work, you are missing intent-filter
<activity
android:name="com.example.test.Counter"
android:label="#string/title_activity_counter" >
<intent-filter>
<action android:name="android.intent.action.COUNTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You should always provide an intent-filer if you want to start an activity with your way ..

you should have t.start();. That is the function that starts a thread.

You may also want to try running on the UI thread by replacing the contents of your finally block with this:
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
Intent counter = new Intent(MyActivity.this, Counter.class);
MyActivity.this.startActivity(counter);
}
};
And as others have said, use t.start() instead of t.run() as run() will block any further action until it completes.

Related

android.content.ActivityNotFoundException: No Activity found to handle Intent

please help , when i run the app, the app launch a Fatal Error im learning
just i create a instat with a call but not run, (sorry for my english)
.......................................................................
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.octa.appprueba3">
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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" />
<category android:name="android.intent.category.APP_CONTACTS" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
my code:
public class SecondActivity extends AppCompatActivity {
private EditText editPhoneText;
private ImageButton imageCallButton;
private final int PHONE_CALL_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editPhoneText = (EditText) findViewById(R.id.editTextPhone);
imageCallButton = (ImageButton) findViewById(R.id.imageCallButton1);
imageCallButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
implicito();
}
});
}
public void implicito(){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("Tel: 9999999" ));
startActivity(intent);
}
}
You may want to check whether your device can handle your intent by doing this:
PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Log.d(TAG, "Cannot handle this intent");
}
What's more, if you are placing a call like so, there is NO need to declare the permission in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Because you are not directly calling someone within your app. You are actually transferring the "duty" to other apps which can handle your intent to call. This doesn't need a permission.
To directly make a call within your app, the intent should be Intent.ACTION_CALL, which needs the permission you declared.
Hope this will help.
There is no activity on your device that handles ACTION_DIAL for a Tel: scheme. Try:
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:9999999" ));
Case and whitespace are important when assembling a Uri.
Also note that even my revised Intent will not work on all devices, such as on an Android tablet that lacks a dialer.

Running code on boot android

I'm having issues getting code to run on boot, I've downloaded the source for an example that should work, but it doesn't. according to the example, it should produce a toast when the phones turned on, but it doesn't happen, I've tested on android 6.0 and 7.0.
any help is appreciated thanks.
The code is as follows:
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidautostartup"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".AutoStartUp" >
</service>
<activity
android:name="com.example.androidautostartup.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>
</application>
</manifest>
BootComplete.java
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
}
AutoStartUp.java
public class AutoStartUp extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
I really wouldn't recommend doing UI stuff on a worker thread (service). While it can be done, it generates confusion for the user to receive a message outside an app context.
Having said that, if you need to do this you should be running UI code on the UI thread.
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getContext(), "Service Started", Toast.LENGTH_LONG).show();
}
});

Android Activity only works if it's declared as main and launcher activity

I'm building a music player in android. What I'm trying is to show playlist
when the app launches and user click on the list item and intent is passed to the android_player activity which plays the song.
But it's not working and when i click on list item app crashes and closes.
But when I declare music_player activity as launcher it works. But I want to show the playlist first and not the music_player.
I have following manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".AndroidMusicPlayerActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait">
<-- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> -->
</activity>
</application>
What changes I should make to my manifest file to make it work.
AndroidMusicPlayerActivity has following code to receive intent
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}
}
MainActivity which shows playlist passes following intent
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listItem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AndroidMusicPlayerActivity.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
Log Cat data, when I click on listitem.
04-30 16:17:16.946: D/OpenGLRenderer(3858): Enabling debug mode 0 04-30
16:17:29.958: W/IInputConnectionWrapper(3858): showStatusIcon on inactive InputConnection
04-30 16:17:30.018: D/OpenGLRenderer(3858): Flushing caches (mode 0)
What changes I should make to make this code work.
I'm new to android. Please help.
Thanks!!
I may be missing something but in your itemClickListener you seem to be calling finish() on your first activity without starting the second.
Surely you need to add a startActivity(in); call.
why you are making both activity as launcher in manifest? remove it then check.

Change page via BroadcastReceiver does not work

I am trying to make a second page show up via a BroadcastReceiver, but the App keeps crashing and I don't know why.
This might be a problem of the correct context.
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.v("$$$$$", "In Method: ACTION_SCREEN_OFF");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.v("$$$$$", "In Method: ACTION_SCREEN_ON");
changeActivity(context);
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Log.v("$$$$$", "In Method: USER_PRESENT");
}
}
public void changeActivity(Context context) {
Intent intent = new Intent(context, Second_Page.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
}
}
My Manifest. In there I declare the class that the log file states is missing:
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".CatchUnlockActivity"
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="second_page" >
</activity>
</application>
</manifest>
Found the solution:
The declaration of the classes is case-sensitive!
Changed every occurrence from "second_page" to "Second_Page" and now it works.

Why do I get a ClassCastException?

I am getting a ClassCastException on running this simple application.
I'm trying to use the AlarmManager for the first time.
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
this is my manisfest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tcs.mine"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AlarmReciever"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
</application>
</manifest>
What am I doing wrong?
AlarmReceiver is not an Activity but declared as one. Check the documentation on BroadcastReceiver and how to declare them in the manifest file. Maybe you want to check this tutorial
The package statement is missing at the top of your java class.
Besides, check how the AlarmReceiver is spelled throughout your .java and .xml files. Somewhere is called AlarmReciever.

Categories

Resources