Start FtpServer when charging the Phone - android

i want to make an App which starts an FTPServer when my Phone is conncted to Power. I dont want my App to have an GUI.
In GooglePlay i found an 3rd Party FtpServer App which can be started and stopped via Intent
Intents:
com.theolivetree.ftpserver.StartFtpServer
com.theolivetree.ftpserver.StopFtpServer
So i would like to use those codes in my app to execute those commands but i dont have any clue how to use them. So far i am trying to use Toast to look if anything happen before i move further, but unfortunally nothig happens.
my AndroidManifest.XML looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.power.ftpserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:allowBackup="true">
<receiver android:enabled="true" android:name="FtpReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"> </action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
FtpReceiver.java
package de.power.ftpserver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class FtpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context , Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED))
{
Toast.makeText(context, "FtpServer gestartet", Toast.LENGTH_LONG).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED))
{
Toast.makeText(context, "FtpServer beendet", Toast.LENGTH_LONG).show();
}
}
}
When i install the App to my Phone the Console says
[2013-08-02 12:28:19 - FtpServer] No Launcher activity found!
[2013-08-02 12:28:19 - FtpServer] The launch will only sync the application package on the device!
but installs anyway. I can Plug in and Out the Powerconnectioncable but i dont get any Toast Messages. Can anyone help plz?

I know this is a bit old. But just for future reference and as a partial solution, I had the same problem of how to use the Intents provided by the FTP Server app, this is what you're looking for:
Intent startIntent = new Intent("com.theolivetree.ftpserver.StartFtpServer");
sendBroadcast(startIntent); // start
And to stop, it's the same
Intent startIntent = new Intent("com.theolivetree.ftpserver.StopFtpServer");
sendBroadcast(startIntent); // stop
Hope it still can help someone.

Related

Application have Only BroadcastReceiver is not working

I have only one CallBroadcastReceiver which extends BroadcastReceiver and a menifest where its declared.
Still its not showing the Toast while I am placing a outgoing call.
Can you please help?
its showing
[2016-02-27 10:02:20 - OnlyReciever] No Launcher activity found!
[2016-02-27 10:02:20 - OnlyReciever] The launch will only sync the application package on the device!
Code Below -
CallBroadcastReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* #author Cosmos
*
*/
public class CallBroadcastReceiver extends BroadcastReceiver
{
public CallBroadcastReceiver() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ind.example.onlyreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="ind.example.onlyreciever.CallBroadcastReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
For Android 3.1 and higher,
You have to launch one of your activities to get your app out of stopped state before any manifest-registered BroadcastReceiver will work, as detailed in the 3.1 release notes.
When the app is first installed or manually force-closed, it is in "stopped state". In this state, only broadcast intents with the FLAG_INCLUDE_STOPPED_PACKAGES flag will reach your broadcast receivers; this flag is not included in the default system broadcasts, so the app cannot receive them in the stopped state.
Note that "stopped state" is tracked by the package manager and is not the same as "application not running". Once the app is out of stopped state, it will remain so even if you reboot your device and your app is not running.

Start APP after Installation via android.provider.Telephony.SMS_RECEIVED [duplicate]

Problem:
I am willing to create an application that simply starts as a background process and whenever a new message comes into the device it should log it into a file or simply display a toast message.
I have read a lot of blogs and tried to follow the steps as mentioned. But, I keep on sending messages on my device and nothing displayed not even in device log. I want to run it on devices from Froyo to Lollipop. So, I am not willing to use new Telephony API which supports API 19 and later versions.
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".SMSHandler">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Source File
package com.abc.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class SMSHandler extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(context, "message initiated",
Toast.LENGTH_LONG);
toast.show();
if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
toast = Toast.makeText(context, "message received",
Toast.LENGTH_LONG);
toast.show();
}
}
}
Environment:
IDE:
Android Studio
Min SDK Version:
8
Tested On:
ICS Device (Sony Xperia U)
Kit-Kat (MOTO G)
You need to add an activity, then run that activity, before this BroadcastReceiver will work.
More accurately, something needs to use an explicit Intent before your app will be moved out of the stopped state and allow manifest-registered BroadcastReceivers to work. The simplest way to do that is to have a launcher activity, and run that activity from the launcher.
To learn more, see "Launch controls on stopped applications" in the Android 3.1 release notes, along with this blog post.
Your Code look like this in manifest file
<receiver android:name=".SMSHandler"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Add the following to your<receiver> in the manifest:
android:enabled="true"
android:exported="true">
Furthermore, according to this thread, it seems that you have to manually start one of your activities before the broadcast receiver will start working, i.e. the application has to have been launched at least once before any broadcast receiver will work.

Android App with Broadcast receiver after reboot as main

Hello i would to develop a simple app without Main Activity as launcher.
I want to register a broadcast receiver which starts after reboot of device and inside OnReceive callback starts an Activity
Here my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
And here my Broadcast receiver
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
And finally the MainActivity
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What is wrong in my code?
Thanks in advance
Francesco
my code is working..here is it...
in manifest
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I cant see anything wrong with your code, however i have something worth to try.
Move the permission out of the application tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If its not working, simplify the receiver :
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Still not working? Try to add some delay, as mentioned here :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//run your service
}
}, 10000);
Quoted from the link above :
While, I suggestion to delay several seconds, e.g., 10 seconds, before
running the (1) line, which is more stable for different phones and
services.
For example, in my case, my service is going to write sd card. If you
start your service immediately, some phones may fail because the sd
card is not ready.
Starting with android 3.1 you cannot have a broadcast receiver to get spawned by the application service manager if it has no context in active state (aka. at least an activity or service that is keeping the process in an "active state")
Excerpt from specification
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
You need to somehow start your application, and then send it in a dormant state (but registered in the app manager). You can use a service for this.
It is strongly NOT recommended to start Activity from BroadcastReciever:
https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
Never start an Activity in response to an incoming broadcast Intent.
In my case PackageManager.DONT_KILL_APP helped:
https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
I've experimented with MIUI 8 firmware real device Xiaomi Redmi Note 3.
My findings are:
you have to add app to autorun to enable it be fired by broadcast. I've checked it with such serious apps as Viber, WhatsApp.
I've compared with manifest settings (without enabling reciever programmatically):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

BroadcastReceiver dont catch the phone state

I am trying to start a new app, but i need to know about a change in the phone state...
for some reason (i am new to this) i cant catch the broadcast of the change.
thats my code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","yyyyyyyyyyyy");
}
}
and thats my manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
as you can see... very simple.
but i cant see the Log in the onReceive on my LogCat.
any one knows why?
Thanks!
Due to this and this, your BroadcastReceiver won't receive anything. The solution is to design a simple activity for your app that starts at least once after installation, before it starts receiving.
In order for a Manifest based Broadcast Receiver to get registered, you need to run your app by opening it from the app drawer. If you don't do that, then the receiver will not be called by Android. So basically, you need at least one Activity that can start - it does not have to do anything, it just has to be started at least once. Did you do that?
This is true after Android 3.0+

Intercepting outgoing call - what am I missing?

I'm trying to write a simple app to capture the ACTION_NEW_OUTGOING_CALL intent and write some debugging information.
Here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.apis"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="DialerReceiver" android:exported="false" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
</manifest>
And here is the code for DialerReceiver:
package com.example.android.apis;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DialerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
debugOut("arg0: " + arg0.toString());
debugOut("arg1: " + arg1.toString());
debugOut("isOrderedBroadcast = " + isOrderedBroadcast());
}
private static void debugOut(String str) {
Log.i("DialerReceiver", str);
}
}
For reasons that I do not understand, when I install this and initiate an outgoing call, I get the following error:
WARN/ActivityManager(59): Permission Denial:
broadcasting Intent { act=android.intent.action.NEW_OUTGOING_CALL (has extras) }
from com.android.phone (pid=123, uid=1001) requires null
due to receiver com.example.android.apis/com.example.android.apis.DialerReceiver
What gives? It seems like PROCESS_OUTGOING_CALLS should be sufficient.
FWIW, if I change to a notification without permissions (TIMEZONE_CHANGED, for example), this works like a charm.
Thanks in advance.
Answering my own question.
After reviewing my manifest, it seemed like android:exported="false" was incorrect, since Android itself would need to invoke DialerReceiver.
When I changed this to android:export="true", everything worked just fine.
FWIW, I did this against the emulator (API version 8 and version 10 devices).
I am also intercepting outgoing calls to take an action. I also only have the added permission, PROCESS_OUTGOING_FILES, but I did notice that in my DialerReceiver declaration I am setting a priority:
<receiver
android:name="DialerReceiver"
android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
This setup works fine for me. I can post some code from the DialerReceiver class if you need it. Are you testing this against an emulator or a phone? I have only tested against an actual phone. I am not using an emulator.
Hope this helps.

Categories

Resources