广播接收器读取Wifi的SSID,如果是所需的SSID,则启动活动。当活动启动时(来自broadcastreceiver),它什么都不做。
BroadcastReceiver:
package com.example.mughal.tileviewtest.wifi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;
import com.example.mughal.tileviewtest.LoadingActivity;
import com.example.mughal.tileviewtest.Main2Activity;
/**
* Created by Mughal on 4/28/2016.
*/
public class CheckWifi extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getBSSID ();
Intent i=new Intent(context, LoadingActivity.class);
String temp=remove(wifiInfo.getSSID());
String [] A=temp.split("_");
if(A[0].equals("SPS"))
{
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
String remove(String a)
{
String b;
b= a.substring(1, a.length()-1);
return b;
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mughal.tileviewtest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"></activity>
<receiver android:name="com.example.mughal.tileviewtest.wifi.CheckWifi" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<activity android:name=".Main2Activity">
</activity>
<activity
android:name=".LoadingActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_loading"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
loadingActivity: 包com.example.mughal.tileviewtest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Formatter;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.mughal.tileviewtest.databases.DBHelper;
import com.example.mughal.tileviewtest.networking.AsyncGetMap;
import com.example.mughal.tileviewtest.networking.TcpClient;
import com.example.mughal.tileviewtest.networking.TcpHandler;
import com.example.mughal.tileviewtest.objects.Floor;
import com.example.mughal.tileviewtest.objects.Map;
import com.example.mughal.tileviewtest.wifi.CheckWifi;
import java.util.ArrayList;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class LoadingActivity extends AppCompatActivity {
TcpClient mTcpClient;
String mapName;
String ip;
Context C;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
C=getApplicationContext();
/* get connected wifi*/
WifiManager wifiManager = (WifiManager) this.getSystemService(this.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String temp=wifiInfo.getSSID();
temp=temp.substring(1,(temp.length()-1));
String [] A=temp.split("_");
if(A[0].equals("SPS"))
{
mapName=A[1];
wifiInfo.getIpAddress();
String tempIp = Formatter.formatIpAddress(wifiInfo.getIpAddress());
String ipBreakDown[]=tempIp.split("\\.");
ip=(ipBreakDown[0]+"."+ipBreakDown[1]+"."+ipBreakDown[2]+"."+"69");
new ConnectTask().execute(mapName);
}else{
Toast.makeText(this,"NOT WITHIN SPS WIFI RANGE: ",Toast.LENGTH_LONG).show();
this.finish();
}
/* if SPS_ , then ok; match name after _ */
}
private class ConnectTask extends AsyncTask<String,String,TcpClient> {
@Override
protected TcpClient doInBackground(String... params) {
//we create a TCPClient object and
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message)
{
//this method calls the onProgressUpdate
publishProgress(message);
Log.i("Debug", "Input message: " + message);
}
},ip);
mTcpClient.run(params[0]);
return mTcpClient;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.i("onProgressUpdate", values[0]);
String path=values[0];
mTcpClient.stopClient();
/* check db for map with name */
DBHelper dbh = new DBHelper(getApplicationContext());
path=path.substring(0,path.length()-1);
ArrayList<Map> maps = dbh.getMap(mapName);
/* if present,get map from local db*/
if(maps.isEmpty())
{
new AsyncGetMap(getApplicationContext(),mapName,path,ip).execute();
}
else
{
//get floors from db
ArrayList<Integer>floorNumbs = new ArrayList<Integer>();
ArrayList <Integer> TileNumbs = new ArrayList<Integer>();
String [] pathSpl=path.split("-");
String [] pathSp2=pathSpl[0].split(",");
int psF=Integer.parseInt(pathSp2[0]);
int psT=Integer.parseInt(pathSp2[1]);
String [] fmark=pathSpl[1].split("@");
for(int i=0;i<fmark.length;i++){
String [] Tmark=fmark[i].split(":");
String [] Tmark2=Tmark[1].split(",");
int HeyHey = Integer.parseInt(Tmark[0]);
for(int j=0;j<Tmark2.length;j++){
floorNumbs.add(HeyHey);
TileNumbs.add(Integer.parseInt(Tmark2[j]));
}
}
int map_id = maps.get(0).getId();
ArrayList<Floor> floors = dbh.getFloors(map_id);
//marking the parking spot
floors.get((psF-1)).markPS(psT);
for(int i=0;i<floorNumbs.size();i++){
floors.get((floorNumbs.get(i)-1)).changeTileToMarked(TileNumbs.get(i));
}
ArrayList <Integer> hey= new ArrayList <Integer> ();
for(int l=0;l<floors.size();l++){
hey.add(floors.get(l).getRowSeparator());
}
/* add floors as extra,start new activity */
Intent i= new Intent(getApplicationContext(),MainActivity.class);
Bundle x = new Bundle();
x.putIntegerArrayList("RowSeps",hey);
x.putInt("current",1);
x.putInt("Total",floors.size());
for(int j=0;j<floors.size();j++)
{
int temp=j+1;
String K="floor"+Integer.toString(temp);
x.putIntegerArrayList(K, floors.get(j).getIntArraylist());
}
i.putExtra("MeraBundle",x);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
}
}
}
答案 0 :(得分:-1)
您的上下文是指当前正在运行的活动。它的工作原理是因为LoadingActivity是一个活动,因此它有一个可以传递的上下文,一个对它的引用。您正尝试从非活动类CheckWifi启动一个活动,因此onReceive中的上下文将为空,它不会引用活动。
如果您使用Google搜索了Android提供的BroadcastReceiver文档,您会遇到这样的信息:
&#34; BroadcastReceiver无法查看或捕获与startActivity()一起使用的Intent;同样,当你广播一个意图时,你永远不会找到或开始一个活动。&#34;
答案 1 :(得分:-1)
我认为您无法从CONNECTIVITY_CHANGE broadcastreceiver发起活动。这个广播发生在后台。您可以做的是从接收者创建通知,并在用户选择通知时启动您的活动。